From dd718110454d40c38e35e62596345aa6bfc63fe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Sat, 12 Oct 2024 02:46:10 +0200 Subject: [PATCH] Refactor variable names for logging in auth.py and log.py --- server/src/uds/core/auths/auth.py | 4 +- server/src/uds/core/util/log.py | 4 +- server/src/uds/reports/auto/__init__.py | 4 +- .../uds/transports/X2GO/scripts/authorize.py | 52 +++++++++++-------- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/server/src/uds/core/auths/auth.py b/server/src/uds/core/auths/auth.py index b8e27a0bc..638022827 100644 --- a/server/src/uds/core/auths/auth.py +++ b/server/src/uds/core/auths/auth.py @@ -57,7 +57,7 @@ if typing.TYPE_CHECKING: logger = logging.getLogger(__name__) -authLogger = logging.getLogger('authLog') +auth_logger = logging.getLogger('authLog') RT = typing.TypeVar('RT') @@ -506,7 +506,7 @@ def log_login( log_level = types.log.LogLevel.ERROR if as_error else types.log.LogLevel.INFO - authLogger.info( + auth_logger.info( '|'.join( [ authenticator.name, diff --git a/server/src/uds/core/util/log.py b/server/src/uds/core/util/log.py index 57ce7013b..230e9a7db 100644 --- a/server/src/uds/core/util/log.py +++ b/server/src/uds/core/util/log.py @@ -49,7 +49,7 @@ except ImportError: if typing.TYPE_CHECKING: from django.db.models import Model -useLogger = logging.getLogger('useLog') +use_logger = logging.getLogger('useLog') # Pattern for look for date and time in this format: 2023-04-20 04:03:08,776 (and trailing spaces) @@ -84,7 +84,7 @@ def log_use( userServiceName = 'unknown' if userServiceName is None else userServiceName poolName = 'unknown' if poolName is None else poolName - useLogger.info( + use_logger.info( '|'.join( [ type_, diff --git a/server/src/uds/reports/auto/__init__.py b/server/src/uds/reports/auto/__init__.py index fc2daabb5..10db3229b 100644 --- a/server/src/uds/reports/auto/__init__.py +++ b/server/src/uds/reports/auto/__init__.py @@ -53,7 +53,7 @@ ReportAutoModel = typing.Union[ models.Provider, ] -reportAutoModelDct: collections.abc.Mapping[str, type[ReportAutoModel]] = { +REPORT_AUTOMODEL: typing.Final[collections.abc.Mapping[str, type[ReportAutoModel]]] = { 'ServicePool': models.ServicePool, 'Authenticator': models.Authenticator, 'Service': models.Service, @@ -121,7 +121,7 @@ class ReportAuto(Report, metaclass=ReportAutoType): def getModel(self) -> type[ReportAutoModel]: data_source = self.data_source.split('.', maxsplit=1)[0] - return reportAutoModelDct[data_source] + return REPORT_AUTOMODEL[data_source] def init_gui(self) -> None: # Fills datasource diff --git a/server/src/uds/transports/X2GO/scripts/authorize.py b/server/src/uds/transports/X2GO/scripts/authorize.py index ce1204cdd..6c7898fd0 100644 --- a/server/src/uds/transports/X2GO/scripts/authorize.py +++ b/server/src/uds/transports/X2GO/scripts/authorize.py @@ -7,58 +7,66 @@ import os import errno import pwd -def logError(err): +def log_error(err, username: str = None): + with open('/tmp/uds-x2go-error-{}.log'.format(username or None), 'a') as f: + f.write(err) + print(err) -def updateAuthorizedKeys(user, pubKey): +def update_authorized_keys(username, pubKey): # No X2Go server on windows if 'win' in sys.platform: - logError('Not a linux platform') + log_error('Not a linux platform') return - userInfo = pwd.getpwnam(user) + user_info = pwd.getpwnam(username) + user_info. # Create .ssh on user home - home = userInfo.pw_dir.rstrip('/') + home = user_info.pw_dir.rstrip('/') if not os.path.exists(home): # User not found, nothing done - logError('Home folder for user {} not found'.format(user)) + log_error('Home folder for user {} not found'.format(username)) return - uid = userInfo.pw_uid + uid = user_info.pw_uid - sshFolder = '{}/.ssh'.format(home) - if not os.path.exists(sshFolder): + ssh_folder = '{}/.ssh'.format(home) + if not os.path.exists(ssh_folder): try: - os.makedirs(sshFolder, 0o700) - os.chown(sshFolder, uid, -1) + os.makedirs(ssh_folder, 0o700) + os.chown(ssh_folder, uid, -1) except OSError as e: if e.errno != errno.EEXIST: - logError('Error creating .ssh folder for user {}: {}'.format(user, e)) + log_error('Error creating .ssh folder for user {}: {}'.format(username, e), username) return # Folder has been created in between test & creation, thats ok - authorizedKeys = '{}/authorized_keys'.format(sshFolder) + authorized_keys = '{}/authorized_keys'.format(ssh_folder) try: - with open(authorizedKeys, 'r') as f: + with open(authorized_keys, 'r') as f: lines = f.readlines() except Exception: lines = [] - with open(authorizedKeys, 'w') as f: - for line in lines: - if 'UDS@X2GOCLIENT' not in line and line.strip(): - f.write(line) + with open(authorized_keys, 'w') as f: + f.writelines( + filter( + lambda x: 'UDS@X2GOCLIENT' not in x and x.strip(), + lines + ) + ) # Append pubkey f.write('ssh-rsa {} UDS@X2GOCLIENT\n'.format(pubKey)) # Ensure access is correct - os.chown(authorizedKeys, uid, -1) - os.chmod(authorizedKeys, 0o600) + os.chown(authorized_keys, uid, -1) + os.chmod(authorized_keys, 0o600) # Done -# __USER__ and __KEY__ will be replaced by the real values, they are placeholders (and must be left as is) -updateAuthorizedKeys('__USER__', '__KEY__') +# __USER__ and __KEY__ will be replaced by the real values, +# # they are placeholders for the real values so keep them. +update_authorized_keys('__USER__', '__KEY__')