From 6b2f9df247fc24e57c5c6652c1616aca71b67a07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Thu, 3 Oct 2024 21:37:34 +0200 Subject: [PATCH] Fixing up unmanaged meshed things :S --- src/actor_config.py | 2 +- src/actor_config_unmanaged.py | 4 +-- src/udsactor/http/public.py | 2 +- src/udsactor/linux/service.py | 2 +- src/udsactor/linux/store.py | 2 +- src/udsactor/macos/service.py | 2 +- src/udsactor/rest.py | 1 + src/udsactor/service.py | 53 ++++++++++++++------------------- src/udsactor/types.py | 1 + src/udsactor/windows/service.py | 2 +- 10 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/actor_config.py b/src/actor_config.py index c9470dd..ccaf875 100755 --- a/src/actor_config.py +++ b/src/actor_config.py @@ -158,7 +158,7 @@ def registerWithUDS(self) -> None: self.ui.logLevelComboBox.currentIndex() # Loglevel ) # Store parameters on register for later use, notify user of registration - udsactor.platform.store.writeConfig( + udsactor.platform.store.write_config( udsactor.types.ActorConfigurationType( actorType=udsactor.types.MANAGED, host=self.ui.host.text(), diff --git a/src/actor_config_unmanaged.py b/src/actor_config_unmanaged.py index 211ce67..b5e5081 100755 --- a/src/actor_config_unmanaged.py +++ b/src/actor_config_unmanaged.py @@ -148,7 +148,7 @@ def saveConfig(self) -> None: log_level=self.ui.logLevelComboBox.currentIndex(), ) - udsactor.platform.store.writeConfig(self._config) + udsactor.platform.store.write_config(self._config) # Enables test button self.ui.testButton.setEnabled(True) # Informs the user @@ -186,7 +186,7 @@ def saveConfig(self) -> None: try: with open(sys.argv[2], 'rb') as import_: config = pickle.load(import_) # nosec: B301: the file is provided by user, so it's not a security issue - udsactor.platform.store.writeConfig(config) + udsactor.platform.store.write_config(config) except Exception as e: print('Error importing configuration file: {}'.format(e)) sys.exit(1) diff --git a/src/udsactor/http/public.py b/src/udsactor/http/public.py index ae9ad5c..3a11a5f 100644 --- a/src/udsactor/http/public.py +++ b/src/udsactor/http/public.py @@ -89,6 +89,6 @@ def get_screenshot(self) -> typing.Any: return self._service._clientsPool.screenshot() # pylint: disable=protected-access def get_uuid(self) -> typing.Any: - if self._service.isManaged(): + if self._service.is_managed(): return self._service._cfg.own_token # pylint: disable=protected-access return '' diff --git a/src/udsactor/linux/service.py b/src/udsactor/linux/service.py index a8b9bc2..eb1bc18 100644 --- a/src/udsactor/linux/service.py +++ b/src/udsactor/linux/service.py @@ -74,7 +74,7 @@ def run(self) -> None: # Linux daemon will continue running unless something is requested to # Unmanaged services does not initializes "on start", but rather when user logs in (because userservice does not exists "as such" before that) - if self.isManaged(): + if self.is_managed(): if not self.initialize(): self.finish() return # Stop daemon if initializes told to do so diff --git a/src/udsactor/linux/store.py b/src/udsactor/linux/store.py index b0f0755..d91f39d 100644 --- a/src/udsactor/linux/store.py +++ b/src/udsactor/linux/store.py @@ -81,7 +81,7 @@ def readConfig() -> types.ActorConfigurationType: return types.ActorConfigurationType('', False) -def writeConfig(config: types.ActorConfigurationType) -> None: +def write_config(config: types.ActorConfigurationType) -> None: cfg = configparser.ConfigParser() cfg.add_section('uds') uds: configparser.SectionProxy = cfg['uds'] diff --git a/src/udsactor/macos/service.py b/src/udsactor/macos/service.py index bbbeae9..f4022da 100644 --- a/src/udsactor/macos/service.py +++ b/src/udsactor/macos/service.py @@ -65,7 +65,7 @@ def run(self) -> None: # Linux daemon will continue running unless something is requested to # Unmanaged services does not initializes "on start", but rather when user logs in (because userservice does not exists "as such" before that) - if self.isManaged(): # Currently, managed is not implemented for UDS on M + if self.is_managed(): # Currently, managed is not implemented for UDS on M logger.error('Managed machines not supported on MacOS') # Wait a bit, this is mac os and will be run by launchd # If the daemon shuts down too quickly, launchd may think it is a crash. diff --git a/src/udsactor/rest.py b/src/udsactor/rest.py index 2bd6263..2824834 100644 --- a/src/udsactor/rest.py +++ b/src/udsactor/rest.py @@ -280,6 +280,7 @@ def initialize( custom[name] = os[i] # os[i] is present, so force it on custom return types.InitializationResultType( + master_token=r['master_token'], token=r['token'], unique_id=r['unique_id'].lower() if r['unique_id'] else None, os=types.ActorOsConfigurationType( diff --git a/src/udsactor/service.py b/src/udsactor/service.py index f1e12d6..2ebbf1f 100644 --- a/src/udsactor/service.py +++ b/src/udsactor/service.py @@ -114,7 +114,7 @@ def startHttpServer(self): self._http = server.HTTPServerThread(self) self._http.start() - def isManaged(self) -> bool: + def is_managed(self) -> bool: return ( self._cfg.actorType != types.UNMANAGED ) # Only "unmanaged" hosts are unmanaged, the rest are "managed" @@ -142,7 +142,7 @@ def reboot(self) -> None: self._rebootRequested = True def setReady(self) -> None: - if not self._isAlive or not self.isManaged(): + if not self._isAlive or not self.is_managed(): return # Unamanged actor types does not set ready never (has no osmanagers, no needing for this) @@ -150,7 +150,7 @@ def setReady(self) -> None: if self._cfg.post_command: self.execute(self._cfg.post_command, 'postConfig') self._cfg = self._cfg._replace(post_command=None) - platform.store.writeConfig(self._cfg) + platform.store.write_config(self._cfg) if self._cfg.own_token and self._interfaces: srvInterface = self.serviceInterfaceInfo() @@ -190,8 +190,7 @@ def setReady(self) -> None: if self._cfg.config: self._cfg = self._cfg._replace(config=self._cfg.config._replace(os=None), data=None) - platform.store.writeConfig(self._cfg) - + platform.store.write_config(self._cfg) logger.info('Service ready') @@ -199,7 +198,7 @@ def configureMachine(self) -> bool: if not self._isAlive: return False - if not self.isManaged(): + if not self.is_managed(): return True # First, if runonce is present, honor it and remove it from config @@ -207,7 +206,7 @@ def configureMachine(self) -> bool: if self._cfg.runonce_command: runOnce = self._cfg.runonce_command self._cfg = self._cfg._replace(runonce_command=None) - platform.store.writeConfig(self._cfg) + platform.store.write_config(self._cfg) if self.execute(runOnce, "runOnce"): # If runonce is present, will not do anythin more # So we have to ensure that, when runonce command is finished, reboots the machine. @@ -285,7 +284,7 @@ def initialize(self) -> bool: self._initialized = True # Force time sync, just in case... - if self.isManaged(): + if self.is_managed(): platform.operations.forceTimeSync() # Wait for Broker to be ready @@ -296,33 +295,31 @@ def initialize(self) -> bool: try: # If master token is present, initialize and get configuration data if self._cfg.master_token: - initResult: types.InitializationResultType = self._api.initialize( + init_result: types.InitializationResultType = self._api.initialize( self._cfg.master_token, self._interfaces, self._cfg.actorType ) - if not initResult.token: # Not managed - logger.debug( - 'This host is not managed by UDS Broker (ids: {})'.format(self._interfaces) - ) + if not init_result.token: # Not managed + logger.debug('This host is not managed by UDS Broker (ids: %s)', self._interfaces) return False # Only removes master token for managed machines (will need it on next client execution) # For unmanaged, if alias is present, replace master token with it master_token = ( - None if self.isManaged() else (initResult.token or self._cfg.master_token) + None if self.is_managed() else (init_result.master_token or self._cfg.master_token) ) # Replace master token with alias token if present self._cfg = self._cfg._replace( master_token=master_token, - own_token=initResult.token, + own_token=init_result.token, config=types.ActorDataConfigurationType( - unique_id=initResult.unique_id, os=initResult.os + unique_id=init_result.unique_id, os=init_result.os ), ) # On first successfull initialization request, master token will dissapear for managed hosts # so it will be no more available (not needed anyway). For unmanaged, the master token will # be replaced with an alias token. - platform.store.writeConfig(self._cfg) + platform.store.write_config(self._cfg) # Setup logger now if self._cfg.own_token: @@ -370,7 +367,7 @@ def finish(self) -> None: self.notifyStop() def checkIpsChanged(self) -> None: - if not self.isManaged(): + if not self.is_managed(): return # Unamanaged hosts does not changes ips. (The full initialize-login-logout process is done in a row, so at login the IP is correct) try: @@ -455,17 +452,16 @@ def login(self, username: str, sessionType: typing.Optional[str] = None) -> type secret = None # If unmanaged, do initialization now, because we don't know before this # Also, even if not initialized, get a "login" notification token - if not self.isManaged(): + if not self.is_managed(): self._initialized = ( self.initialize() ) # Maybe it's a local login by an unmanaged host.... On real login, will execute initilize again - # Unamanaged, need the master token - master_token = self._cfg.master_token secret = self._secret # Own token will not be set if UDS did not assigned the initialized VM to an user - # In that case, take master token (if machine is Unamanaged version) - token = self._cfg.own_token or master_token + # On "unmanaged", on login the "initialize" method is invoked always, + # so the own token will be set + token = self._cfg.own_token or self._cfg.master_token if token: result = self._api.login( self._cfg.actorType, @@ -476,9 +472,8 @@ def login(self, username: str, sessionType: typing.Optional[str] = None) -> type secret, ) - if ( - result.session_id - ): # If logged in, process it. client_pool will take account of login response to client and session + # If logged in, process it. client_pool will take account of login response to client and session + if result.session_id: script = platform.store.invokeScriptOnLogin() if script: logger.info('Executing script on login: {}'.format(script)) @@ -493,11 +488,9 @@ def logout( session_type: typing.Optional[str], session_id: typing.Optional[str], ) -> None: - master_token = self._cfg.master_token - # Own token will not be set if UDS did not assigned the initialized VM to an user # In that case, take master token (if machine is Unamanaged version) - token = self._cfg.own_token or master_token + token = self._cfg.own_token or self._cfg.master_token if token: # If logout is not processed (that is, not ok result), the logout has not been processed if ( @@ -517,7 +510,7 @@ def logout( self.onLogout(username, session_id or '') - if not self.isManaged(): + if not self.is_managed(): self.uninitialize() # ****************************************************** diff --git a/src/udsactor/types.py b/src/udsactor/types.py index e0e1805..f859203 100644 --- a/src/udsactor/types.py +++ b/src/udsactor/types.py @@ -45,6 +45,7 @@ class ActorConfigurationType(typing.NamedTuple): data: typing.Optional[dict[str, typing.Any]] = None class InitializationResultType(typing.NamedTuple): + master_token: typing.Optional[str] = None token: typing.Optional[str] = None unique_id: typing.Optional[str] = None os: typing.Optional[ActorOsConfigurationType] = None diff --git a/src/udsactor/windows/service.py b/src/udsactor/windows/service.py index e1b5577..c1f4516 100644 --- a/src/udsactor/windows/service.py +++ b/src/udsactor/windows/service.py @@ -258,7 +258,7 @@ def SvcDoRun(self) -> None: # pylint: disable=too-many-statements, too-many-bra return # Unmanaged services does not initializes "on start", but rather when user logs in (because userservice does not exists "as such" before that) - if self.isManaged(): + if self.is_managed(): if not self.initialize(): logger.info('Service stopped due to init') self.finish()