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

Fixed some actor registration related info

This commit is contained in:
Adolfo Gómez García 2024-07-25 03:57:49 +02:00
parent 25c5c3a4d1
commit cdb0f110cf
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
4 changed files with 35 additions and 25 deletions

2
actor

@ -1 +1 @@
Subproject commit 93e46e3d14a6e47ef53736573ae79abcd46fee31 Subproject commit e6b59cf809202b0a9d26ee14abfd19fab13d4817

View File

@ -86,7 +86,7 @@ class Handler:
# These are the "path" split by /, that is, the REST invocation arguments # These are the "path" split by /, that is, the REST invocation arguments
_args: list[str] _args: list[str]
_kwargs: dict[str, typing.Any] # This are the "path" split by /, that is, the REST invocation arguments _kwargs: dict[str, typing.Any] # This are the "path" split by /, that is, the REST invocation arguments
_headers: dict[str, str] _headers: dict[str, str] # Note: These are "output" headers, not input headers (input headers can be retrieved from request)
_session: typing.Optional[SessionStore] _session: typing.Optional[SessionStore]
_auth_token: typing.Optional[str] _auth_token: typing.Optional[str]
_user: 'User' _user: 'User'

View File

@ -61,21 +61,22 @@ class ActorTokens(ModelHandler):
{'stamp': {'title': _('Date'), 'type': 'datetime'}}, {'stamp': {'title': _('Date'), 'type': 'datetime'}},
{'username': {'title': _('Issued by')}}, {'username': {'title': _('Issued by')}},
{'host': {'title': _('Origin')}}, {'host': {'title': _('Origin')}},
{'version': {'title': _('Version')}},
{'hostname': {'title': _('Hostname')}}, {'hostname': {'title': _('Hostname')}},
{'pre_command': {'title': _('Pre-connect')}}, {'pre_command': {'title': _('Pre-connect')}},
{'post_command': {'title': _('Post-Configure')}}, {'post_command': {'title': _('Post-Configure')}},
{'runonce_command': {'title': _('Run Once')}}, {'run_once_command': {'title': _('Run Once')}},
{'log_level': {'title': _('Log level')}}, {'log_level': {'title': _('Log level')}},
{'os': {'title': _('OS')}},
] ]
def item_as_dict(self, item: 'Model') -> dict[str, typing.Any]: def item_as_dict(self, item: 'Model') -> dict[str, typing.Any]:
item = ensure.is_instance(item, Server) item = ensure.is_instance(item, Server)
data: dict[str, typing.Any] = item.data or {} data: dict[str, typing.Any] = item.data or {}
log_level_int = data.get('log_level', 2) if item.log_level < 10000: # Old log level, from actor, etc..
if log_level_int < 10000: # Old log level, from actor, etc.. log_level = LogLevel.from_actor_level(item.log_level).name
log_level = LogLevel.from_actor_level(log_level_int).name
else: else:
log_level = LogLevel(log_level_int).name log_level = LogLevel(item.log_level).name
return { return {
'id': item.token, 'id': item.token,
'name': str(_('Token isued by {} from {}')).format(item.register_username, item.hostname or item.ip), 'name': str(_('Token isued by {} from {}')).format(item.register_username, item.hostname or item.ip),
@ -84,10 +85,12 @@ class ActorTokens(ModelHandler):
'ip': item.ip, 'ip': item.ip,
'host': f'{item.ip} - {data.get("mac")}', 'host': f'{item.ip} - {data.get("mac")}',
'hostname': item.hostname, 'hostname': item.hostname,
'version': item.version,
'pre_command': data.get('pre_command', ''), 'pre_command': data.get('pre_command', ''),
'post_command': data.get('post_command', ''), 'post_command': data.get('post_command', ''),
'runonce_command': data.get('runonce_command', ''), 'run_once_command': data.get('run_once_command', ''),
'log_level': log_level, 'log_level': log_level,
'os': item.os_type,
} }
def delete(self) -> str: def delete(self) -> str:

View File

@ -34,6 +34,7 @@ import logging
import time import time
import typing import typing
import collections.abc import collections.abc
import re
from django.conf import settings from django.conf import settings
@ -277,48 +278,54 @@ class Register(ActorV3Action):
actor_token: typing.Optional[Server] = Server.objects.filter( actor_token: typing.Optional[Server] = Server.objects.filter(
type=types.servers.ServerType.ACTOR, mac=self._params['mac'] type=types.servers.ServerType.ACTOR, mac=self._params['mac']
).first() ).first()
# Try to get version from headers (USer-Agent), should be something like (UDS Actor v(.+))
user_agent = self._request.headers.get('User-Agent', '')
match = re.search(r'UDS Actor v(.+)', user_agent)
if match:
self._params['version'] = self._params.get('version', match.group(1)) # override version if not provided
# Actors does not support any SERVER API version in fact, they has their own interfaces on UserServices # Actors does not support any SERVER API version in fact, they has their own interfaces on UserServices
# This means that we can invoke its API from user_service, but not from server (The actor token is transformed as soon as initialized to a user service token) # This means that we can invoke its API from user_service, but not from server (The actor token is transformed as soon as initialized to a user service token)
data = {
'pre_command': self._params['pre_command'],
'post_command': self._params['post_command'],
'run_once_command': self._params['run_once_command'],
'custom': self._params.get('custom', ''),
}
if actor_token: if actor_token:
# Update parameters # Update parameters
# type is already set
actor_token.subtype = self._params.get('subtype', '')
actor_token.version = self._params.get('version', '')
actor_token.register_username = self._user.pretty_name actor_token.register_username = self._user.pretty_name
actor_token.register_ip = self._request.ip actor_token.register_ip = self._request.ip
actor_token.ip = self._params['ip'] actor_token.ip = self._params['ip']
actor_token.hostname = self._params['hostname'] actor_token.hostname = self._params['hostname']
actor_token.log_level = self._params['log_level'] actor_token.log_level = self._params['log_level']
actor_token.subtype = self._params.get('version', '') actor_token.data = data
actor_token.data = typing.cast(typing.Any, { # Cast due to mypy complains on this assignment
'pre_command': self._params['pre_command'],
'post_command': self._params['post_command'],
'run_once_command': self._params['run_once_command'],
'custom': self._params.get('custom', ''),
})
actor_token.stamp = sql_now() actor_token.stamp = sql_now()
actor_token.os_type = self._params.get('os', types.os.KnownOS.UNKNOWN.os_name())[:31]
# Mac is already set, as type was used to locate it
actor_token.save() actor_token.save()
logger.info('Registered actor %s', self._params) logger.info('Registered actor %s', self._params)
found = True found = True
if not found: if not found:
kwargs = { kwargs = {
'type': types.servers.ServerType.ACTOR,
'subtype': self._params.get('subtype', ''),
'version': self._params.get('version', ''),
'register_username': self._user.pretty_name, 'register_username': self._user.pretty_name,
'register_ip': self._request.ip, 'register_ip': self._request.ip,
'ip': self._params['ip'], 'ip': self._params['ip'],
'hostname': self._params['hostname'], 'hostname': self._params['hostname'],
'log_level': self._params['log_level'], 'log_level': self._params['log_level'],
'data': { 'data': data,
'pre_command': self._params['pre_command'],
'post_command': self._params['post_command'],
'run_once_command': self._params['run_once_command'],
'custom': self._params.get('custom', ''),
},
# 'token': Server.create_token(), # Not needed, defaults to create_token # 'token': Server.create_token(), # Not needed, defaults to create_token
'type': types.servers.ServerType.ACTOR, 'stamp': sql_now(),
'subtype': self._params.get('version', ''),
'version': '',
'os_type': self._params.get('os', types.os.KnownOS.UNKNOWN.os_name()), 'os_type': self._params.get('os', types.os.KnownOS.UNKNOWN.os_name()),
'mac': self._params['mac'], 'mac': self._params['mac'],
'stamp': sql_now(),
} }
actor_token = Server.objects.create(**kwargs) actor_token = Server.objects.create(**kwargs)