1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

Refactor variable names for logging in auth.py and log.py

This commit is contained in:
Adolfo Gómez García 2024-10-12 02:46:10 +02:00
parent c42b5f270b
commit dd71811045
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
4 changed files with 36 additions and 28 deletions

View File

@ -57,7 +57,7 @@ if typing.TYPE_CHECKING:
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
authLogger = logging.getLogger('authLog') auth_logger = logging.getLogger('authLog')
RT = typing.TypeVar('RT') 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 log_level = types.log.LogLevel.ERROR if as_error else types.log.LogLevel.INFO
authLogger.info( auth_logger.info(
'|'.join( '|'.join(
[ [
authenticator.name, authenticator.name,

View File

@ -49,7 +49,7 @@ except ImportError:
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
from django.db.models import Model 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) # 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 userServiceName = 'unknown' if userServiceName is None else userServiceName
poolName = 'unknown' if poolName is None else poolName poolName = 'unknown' if poolName is None else poolName
useLogger.info( use_logger.info(
'|'.join( '|'.join(
[ [
type_, type_,

View File

@ -53,7 +53,7 @@ ReportAutoModel = typing.Union[
models.Provider, models.Provider,
] ]
reportAutoModelDct: collections.abc.Mapping[str, type[ReportAutoModel]] = { REPORT_AUTOMODEL: typing.Final[collections.abc.Mapping[str, type[ReportAutoModel]]] = {
'ServicePool': models.ServicePool, 'ServicePool': models.ServicePool,
'Authenticator': models.Authenticator, 'Authenticator': models.Authenticator,
'Service': models.Service, 'Service': models.Service,
@ -121,7 +121,7 @@ class ReportAuto(Report, metaclass=ReportAutoType):
def getModel(self) -> type[ReportAutoModel]: def getModel(self) -> type[ReportAutoModel]:
data_source = self.data_source.split('.', maxsplit=1)[0] data_source = self.data_source.split('.', maxsplit=1)[0]
return reportAutoModelDct[data_source] return REPORT_AUTOMODEL[data_source]
def init_gui(self) -> None: def init_gui(self) -> None:
# Fills datasource # Fills datasource

View File

@ -7,58 +7,66 @@ import os
import errno import errno
import pwd 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) print(err)
def updateAuthorizedKeys(user, pubKey): def update_authorized_keys(username, pubKey):
# No X2Go server on windows # No X2Go server on windows
if 'win' in sys.platform: if 'win' in sys.platform:
logError('Not a linux platform') log_error('Not a linux platform')
return return
userInfo = pwd.getpwnam(user) user_info = pwd.getpwnam(username)
user_info.
# Create .ssh on user home # 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 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 return
uid = userInfo.pw_uid uid = user_info.pw_uid
sshFolder = '{}/.ssh'.format(home) ssh_folder = '{}/.ssh'.format(home)
if not os.path.exists(sshFolder): if not os.path.exists(ssh_folder):
try: try:
os.makedirs(sshFolder, 0o700) os.makedirs(ssh_folder, 0o700)
os.chown(sshFolder, uid, -1) os.chown(ssh_folder, uid, -1)
except OSError as e: except OSError as e:
if e.errno != errno.EEXIST: 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 return
# Folder has been created in between test & creation, thats ok # Folder has been created in between test & creation, thats ok
authorizedKeys = '{}/authorized_keys'.format(sshFolder) authorized_keys = '{}/authorized_keys'.format(ssh_folder)
try: try:
with open(authorizedKeys, 'r') as f: with open(authorized_keys, 'r') as f:
lines = f.readlines() lines = f.readlines()
except Exception: except Exception:
lines = [] lines = []
with open(authorizedKeys, 'w') as f: with open(authorized_keys, 'w') as f:
for line in lines: f.writelines(
if 'UDS@X2GOCLIENT' not in line and line.strip(): filter(
f.write(line) lambda x: 'UDS@X2GOCLIENT' not in x and x.strip(),
lines
)
)
# Append pubkey # Append pubkey
f.write('ssh-rsa {} UDS@X2GOCLIENT\n'.format(pubKey)) f.write('ssh-rsa {} UDS@X2GOCLIENT\n'.format(pubKey))
# Ensure access is correct # Ensure access is correct
os.chown(authorizedKeys, uid, -1) os.chown(authorized_keys, uid, -1)
os.chmod(authorizedKeys, 0o600) os.chmod(authorized_keys, 0o600)
# Done # Done
# __USER__ and __KEY__ will be replaced by the real values, they are placeholders (and must be left as is) # __USER__ and __KEY__ will be replaced by the real values,
updateAuthorizedKeys('__USER__', '__KEY__') # # they are placeholders for the real values so keep them.
update_authorized_keys('__USER__', '__KEY__')