1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-13 13:17:54 +03:00

advancing on refactoring and cache improvements

This commit is contained in:
Adolfo Gómez García 2024-01-08 15:26:08 +01:00
parent bc61814ec4
commit cf4a7e0ef9
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
14 changed files with 22 additions and 22 deletions

View File

@ -392,7 +392,7 @@ class OAuth2Authenticator(auths.Authenticator):
groups.extend(self.commonGroups.value.split(',')) groups.extend(self.commonGroups.value.split(','))
# store groups for this username at storage, so we can check it at a later stage # store groups for this username at storage, so we can check it at a later stage
self.storage.putPickle(username, [realName, groups]) self.storage.put_pickle(username, [realName, groups])
# Validate common groups # Validate common groups
gm.validate(groups) gm.validate(groups)

View File

@ -156,7 +156,7 @@ class RadiusAuth(auths.Authenticator):
request.session[client.STATE_VAR_NAME] = state.decode() request.session[client.STATE_VAR_NAME] = state.decode()
# store the user mfa attribute if it is set # store the user mfa attribute if it is set
if mfaCode: if mfaCode:
self.storage.putPickle( self.storage.put_pickle(
self.mfaStorageKey(username), self.mfaStorageKey(username),
mfaCode, mfaCode,
) )

View File

@ -506,7 +506,7 @@ class RegexLdap(auths.Authenticator):
# store the user mfa attribute if it is set # store the user mfa attribute if it is set
if self._mfaAttr: if self._mfaAttr:
self.storage.putPickle( self.storage.put_pickle(
self.mfaStorageKey(username), self.mfaStorageKey(username),
usr[self._mfaAttr][0], usr[self._mfaAttr][0],
) )

View File

@ -675,11 +675,11 @@ class SAMLAuthenticator(auths.Authenticator):
logger.debug('Real name: %s', realName) logger.debug('Real name: %s', realName)
# store groups for this username at storage, so we can check it at a later stage # store groups for this username at storage, so we can check it at a later stage
self.storage.putPickle(username, [realName, groups]) self.storage.put_pickle(username, [realName, groups])
# store also the mfa identifier field value, in case we have provided it # store also the mfa identifier field value, in case we have provided it
if self.mfaAttr.value.strip(): if self.mfaAttr.value.strip():
self.storage.putPickle( self.storage.put_pickle(
self.mfaStorageKey(username), self.mfaStorageKey(username),
''.join(auth_utils.processRegexField(self.mfaAttr.value, attributes)), ''.join(auth_utils.processRegexField(self.mfaAttr.value, attributes)),
) # in case multipel values is returned, join them ) # in case multipel values is returned, join them

View File

@ -459,7 +459,7 @@ class SimpleLDAPAuthenticator(auths.Authenticator):
# store the user mfa attribute if it is set # store the user mfa attribute if it is set
if self._mfaAttr: if self._mfaAttr:
self.storage.putPickle( self.storage.put_pickle(
self.mfaStorageKey(username), self.mfaStorageKey(username),
user[self._mfaAttr][0], user[self._mfaAttr][0],
) )

View File

@ -229,7 +229,7 @@ class MFA(Module):
Internal method to put the data into storage Internal method to put the data into storage
""" """
storageKey = request.ip + userId storageKey = request.ip + userId
self.storage.putPickle(storageKey, (sql_datetime(), code)) self.storage.put_pickle(storageKey, (sql_datetime(), code))
def process( def process(
self, self,

View File

@ -439,7 +439,7 @@ class Service(Module):
return return
def store_id_info(self, id: str, data: typing.Any) -> None: def store_id_info(self, id: str, data: typing.Any) -> None:
self.storage.putPickle('__nfo_' + id, data) self.storage.put_pickle('__nfo_' + id, data)
def recover_id_info(self, id: str, delete: bool = False) -> typing.Any: def recover_id_info(self, id: str, delete: bool = False) -> typing.Any:
# recovers the information # recovers the information

View File

@ -256,7 +256,7 @@ class Storage:
def put(self, skey: typing.Union[str, bytes], data: typing.Any) -> None: def put(self, skey: typing.Union[str, bytes], data: typing.Any) -> None:
return self.saveData(skey, data) return self.saveData(skey, data)
def putPickle( def put_pickle(
self, self,
skey: typing.Union[str, bytes], skey: typing.Union[str, bytes],
data: typing.Any, data: typing.Any,

View File

@ -135,7 +135,7 @@ class TOTP_MFA(mfas.MFA):
return data return data
def _saveUserData(self, userId: str, data: tuple[str, bool]) -> None: def _saveUserData(self, userId: str, data: tuple[str, bool]) -> None:
self.storage.putPickle(userId, data) self.storage.put_pickle(userId, data)
def _removeUserData(self, userId: str) -> None: def _removeUserData(self, userId: str) -> None:
self.storage.remove(userId) self.storage.remove(userId)

View File

@ -154,7 +154,7 @@ class TelegramNotifier(messaging.Notifier):
chatIds = self.storage.getPickle('chatIds') or [] chatIds = self.storage.getPickle('chatIds') or []
if chatId not in chatIds: if chatId not in chatIds:
chatIds.append(chatId) chatIds.append(chatId)
self.storage.putPickle('chatIds', chatIds) self.storage.put_pickle('chatIds', chatIds)
logger.info('User %s subscribed to notifications', chatId) logger.info('User %s subscribed to notifications', chatId)
def unsubscriteUser(self, chatId: int) -> None: def unsubscriteUser(self, chatId: int) -> None:
@ -163,7 +163,7 @@ class TelegramNotifier(messaging.Notifier):
chatIds = self.storage.getPickle('chatIds') or [] chatIds = self.storage.getPickle('chatIds') or []
if chatId in chatIds: if chatId in chatIds:
chatIds.remove(chatId) chatIds.remove(chatId)
self.storage.putPickle('chatIds', chatIds) self.storage.put_pickle('chatIds', chatIds)
logger.info('User %s unsubscribed from notifications', chatId) logger.info('User %s unsubscribed from notifications', chatId)
def retrieveMessages(self) -> None: def retrieveMessages(self) -> None:
@ -176,14 +176,14 @@ class TelegramNotifier(messaging.Notifier):
# If last check is not set, we will set it to now # If last check is not set, we will set it to now
if lastCheck is None: if lastCheck is None:
lastCheck = now - datetime.timedelta(seconds=self.checkDelay.num() + 1) lastCheck = now - datetime.timedelta(seconds=self.checkDelay.num() + 1)
self.storage.putPickle('lastCheck', lastCheck) self.storage.put_pickle('lastCheck', lastCheck)
# If not enough time has passed, we will not check # If not enough time has passed, we will not check
if lastCheck + datetime.timedelta(seconds=self.checkDelay.num()) > now: if lastCheck + datetime.timedelta(seconds=self.checkDelay.num()) > now:
return return
# Update last check # Update last check
self.storage.putPickle('lastCheck', now) self.storage.put_pickle('lastCheck', now)
lastOffset = self.storage.getPickle('lastOffset') or 0 lastOffset = self.storage.getPickle('lastOffset') or 0
t = telegram.Telegram(self.accessToken.value, last_offset=lastOffset) t = telegram.Telegram(self.accessToken.value, last_offset=lastOffset)
@ -207,4 +207,4 @@ class TelegramNotifier(messaging.Notifier):
elif message in ('/leave', '/unsubscribe'): elif message in ('/leave', '/unsubscribe'):
self.unsubscriteUser(update.chat.id) self.unsubscriteUser(update.chat.id)
t.send_message(update.chat.id, _('You have been unsubscribed from notifications')) t.send_message(update.chat.id, _('You have been unsubscribed from notifications'))
self.storage.putPickle('lastOffset', t.lastOffset) self.storage.put_pickle('lastOffset', t.lastOffset)

View File

@ -280,7 +280,7 @@ class IPMachinesService(IPServiceBase):
and self.cache.get('port{}'.format(theIP)) and self.cache.get('port{}'.format(theIP))
): ):
continue # The check failed not so long ago, skip it... continue # The check failed not so long ago, skip it...
self.storage.putPickle(theIP, now) self.storage.put_pickle(theIP, now)
# Is WOL enabled? # Is WOL enabled?
wolENABLED = bool(self.parent().wolURL(theIP, theMAC)) wolENABLED = bool(self.parent().wolURL(theIP, theMAC))
# Now, check if it is available on port, if required... # Now, check if it is available on port, if required...
@ -335,7 +335,7 @@ class IPMachinesService(IPServiceBase):
now = sql_stamp_seconds() now = sql_stamp_seconds()
locked = self.storage.getPickle(theIP) locked = self.storage.getPickle(theIP)
if self.canBeUsed(locked, now): if self.canBeUsed(locked, now):
self.storage.putPickle(theIP, now) self.storage.put_pickle(theIP, now)
if theMAC: if theMAC:
theIP += ';' + theMAC theIP += ';' + theMAC
return userServiceInstance.assign(theIP) return userServiceInstance.assign(theIP)
@ -353,7 +353,7 @@ class IPMachinesService(IPServiceBase):
now = sql_stamp_seconds() now = sql_stamp_seconds()
locked: typing.Union[None, str, int] = self.storage.getPickle(theIP) locked: typing.Union[None, str, int] = self.storage.getPickle(theIP)
if self.canBeUsed(locked, now): if self.canBeUsed(locked, now):
self.storage.putPickle(theIP, str(now)) # Lock it self.storage.put_pickle(theIP, str(now)) # Lock it
def process_logout(self, id: str, remote_login: bool) -> None: def process_logout(self, id: str, remote_login: bool) -> None:
''' '''

View File

@ -90,7 +90,7 @@ class IPSingleMachineService(IPServiceBase):
try: try:
counter = self.storage.getPickle('counter') counter = self.storage.getPickle('counter')
counter = counter + 1 if counter is not None else 1 counter = counter + 1 if counter is not None else 1
self.storage.putPickle('counter', counter) self.storage.put_pickle('counter', counter)
ip = '{}~{}'.format(self.ip.value, counter) ip = '{}~{}'.format(self.ip.value, counter)
except Exception: except Exception:
ip = None ip = None

View File

@ -449,7 +449,7 @@ if sys.platform == 'win32':
self.__setTask(self.service().shutdownMachine(int(self._vmid))) self.__setTask(self.service().shutdownMachine(int(self._vmid)))
shutdown = sql_stamp_seconds() shutdown = sql_stamp_seconds()
logger.debug('Stoped vm using guest tools') logger.debug('Stoped vm using guest tools')
self.storage.putPickle('shutdown', shutdown) self.storage.put_pickle('shutdown', shutdown)
return State.RUNNING return State.RUNNING
def __updateVmMacAndHA(self) -> str: def __updateVmMacAndHA(self) -> str:
@ -537,7 +537,7 @@ if sys.platform == 'win32':
f'Could not shutdown machine using soft power off in time ({GUEST_SHUTDOWN_WAIT} seconds). Powering off.', f'Could not shutdown machine using soft power off in time ({GUEST_SHUTDOWN_WAIT} seconds). Powering off.',
) )
# Not stopped by guest in time, but must be stopped normally # Not stopped by guest in time, but must be stopped normally
self.storage.putPickle('shutdown', 0) self.storage.put_pickle('shutdown', 0)
return self.__stopMachine() # Launch "hard" stop return self.__stopMachine() # Launch "hard" stop
return State.RUNNING return State.RUNNING

View File

@ -50,7 +50,7 @@ class StorageTest(UDSTestCase):
storage.put(b'key', UNICODE_CHARS) storage.put(b'key', UNICODE_CHARS)
storage.put(UNICODE_CHARS_2, UNICODE_CHARS) storage.put(UNICODE_CHARS_2, UNICODE_CHARS)
storage.putPickle('pickle', VALUE_1) storage.put_pickle('pickle', VALUE_1)
self.assertEqual(storage.get(UNICODE_CHARS), u'chars') # Always returns unicod self.assertEqual(storage.get(UNICODE_CHARS), u'chars') # Always returns unicod
self.assertEqual(storage.readData('saveData'), UNICODE_CHARS) self.assertEqual(storage.readData('saveData'), UNICODE_CHARS)