Skip to content

Commit

Permalink
Fixing up unmanaged meshed things :S
Browse files Browse the repository at this point in the history
  • Loading branch information
dkmstr committed Oct 3, 2024
1 parent f2b6254 commit 6b2f9df
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/actor_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions src/actor_config_unmanaged.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/udsactor/http/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ''
2 changes: 1 addition & 1 deletion src/udsactor/linux/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/udsactor/linux/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
2 changes: 1 addition & 1 deletion src/udsactor/macos/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/udsactor/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
53 changes: 23 additions & 30 deletions src/udsactor/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -142,15 +142,15 @@ 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)

# First, if postconfig is available, execute it and disable it
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()
Expand Down Expand Up @@ -190,24 +190,23 @@ 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')

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
# Return values is "True" for keep service (or daemon) running, False if Stop it.
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.
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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))
Expand All @@ -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 (
Expand All @@ -517,7 +510,7 @@ def logout(

self.onLogout(username, session_id or '')

if not self.isManaged():
if not self.is_managed():
self.uninitialize()

# ******************************************************
Expand Down
1 change: 1 addition & 0 deletions src/udsactor/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/udsactor/windows/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 6b2f9df

Please sign in to comment.