Skip to content

Commit

Permalink
* Added support for enabling client_logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dkmstr committed Nov 13, 2024
1 parent 78aefbc commit 553bff6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 24 deletions.
13 changes: 10 additions & 3 deletions server/src/uds/REST/methods/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@
from uds.core.exceptions.services import ServiceNotReadyError
from uds.core.types.log import LogLevel, LogSource
from uds.core.util.config import GlobalConfig
from uds.core.util.model import sql_stamp_seconds
from uds.core.util.rest.tools import match
from uds.models import TicketStore, User
from uds.REST import Handler

logger = logging.getLogger(__name__)

CLIENT_VERSION = consts.system.VERSION
CLIENT_VERSION: typing.Final[str] = consts.system.VERSION
LOG_ENABLED_DURATION: typing.Final[int] = 2 * 60 * 60 * 24 # 2 days


# Enclosed methods under /client path
Expand Down Expand Up @@ -162,8 +164,13 @@ def process(self, ticket: str, scrambler: str) -> dict[str, typing.Any]:

logger.debug('Script: %s', transport_script)

# is_logging_enabled = self._request.user.properties.get('log_enabled', False)
is_logging_enabled = True
# Log is enabled if user has log_enabled property set to
try:
log_enabled_since_limit = sql_stamp_seconds() - LOG_ENABLED_DURATION
log_enabled_since = self._request.user.properties.get('client_logging', log_enabled_since_limit)
is_logging_enabled = False if log_enabled_since <= log_enabled_since_limit else True
except Exception:
is_logging_enabled = False
log: dict[str, 'str|None'] = {
'level': 'DEBUG',
'ticket': None,
Expand Down
38 changes: 31 additions & 7 deletions server/src/uds/REST/methods/users_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

from uds.core.auths.user import User as AUser
from uds.core.util import log, ensure
from uds.core.util.model import process_uuid
from uds.core.util.model import process_uuid, sql_stamp_seconds
from uds.models import Authenticator, User, Group, ServicePool
from uds.core.managers.crypto import CryptoManager
from uds.core import consts, exceptions, types
Expand Down Expand Up @@ -78,7 +78,13 @@ def get_service_pools_for_groups(


class Users(DetailHandler):
custom_methods = ['servicesPools', 'userServices', 'clean_related', 'addToGroup']
custom_methods = [
'services_pools',
'user_services',
'clean_related',
'add_to_group',
'enable_client_logging',
]

def get_items(self, parent: 'Model', item: typing.Optional[str]) -> typing.Any:
parent = ensure.is_instance(parent, Authenticator)
Expand Down Expand Up @@ -286,7 +292,7 @@ def delete_item(self, parent: 'Model', item: str) -> None:
logger.error('Error on user removal of %s.%s: %s', parent.name, item, e)
raise self.invalid_item_response() from e

def servicesPools(self, parent: 'Model', item: str) -> list[dict[str, typing.Any]]:
def services_pools(self, parent: 'Model', item: str) -> list[dict[str, typing.Any]]:
parent = ensure.is_instance(parent, Authenticator)
uuid = process_uuid(item)
user = parent.users.get(uuid=process_uuid(uuid))
Expand All @@ -307,7 +313,7 @@ def servicesPools(self, parent: 'Model', item: str) -> list[dict[str, typing.Any

return res

def userServices(self, parent: 'Authenticator', item: str) -> list[dict[str, typing.Any]]:
def user_services(self, parent: 'Authenticator', item: str) -> list[dict[str, typing.Any]]:
parent = ensure.is_instance(parent, Authenticator)
uuid = process_uuid(item)
user = parent.users.get(uuid=process_uuid(uuid))
Expand All @@ -326,17 +332,35 @@ def clean_related(self, parent: 'Authenticator', item: str) -> dict[str, str]:
user = parent.users.get(uuid=process_uuid(uuid))
user.clean_related_data()
return {'status': 'ok'}
def addToGroup(self, parent: 'Authenticator', item: str) -> dict[str, str]:

def add_to_group(self, parent: 'Authenticator', item: str) -> dict[str, str]:
uuid = process_uuid(item)
user = parent.users.get(uuid=process_uuid(uuid))
group = parent.groups.get(uuid=process_uuid(self._params['group']))
user.log(
f'Added to group {group.name} by {self._user.pretty_name}',
types.log.LogLevel.INFO,
types.log.LogSource.REST,
)
user.groups.add(group)
return {'status': 'ok'}

def enable_client_logging(self, parent: 'Model', item: str) -> dict[str, str]:
parent = ensure.is_instance(parent, Authenticator)
user = parent.users.get(uuid=process_uuid(item))
user.log(
f'Client logging enabled by {self._user.pretty_name}',
types.log.LogLevel.INFO,
types.log.LogSource.REST,
)
with user.properties as props:
props['client_logging'] = sql_stamp_seconds()

return {'status': 'ok'}


class Groups(DetailHandler):
custom_methods = ['servicesPools', 'users']
custom_methods = ['services_pools', 'users']

def get_items(self, parent: 'Model', item: typing.Optional[str]) -> types.rest.ManyItemsDictType:
parent = ensure.is_instance(parent, Authenticator)
Expand Down
26 changes: 13 additions & 13 deletions server/src/uds/static/admin/main.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions server/src/uds/static/admin/translations-fakejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ gettext("Assigned services");
gettext("Information");
gettext("Information");
gettext("Clean related (mfa,...)");
gettext("Enable client logging");
gettext("In Maintenance");
gettext("Active");
gettext("Delete user");
Expand All @@ -149,6 +150,10 @@ gettext("Clean data");
gettext("Clean related data (mfa, ...)?");
gettext("Related data cleaned");
gettext("dismiss");
gettext("Client logging");
gettext("Enable client logging for user?");
gettext("Client logging enabled");
gettext("dismiss");
gettext("Match mode");
gettext("Any");
gettext("All");
Expand Down
2 changes: 1 addition & 1 deletion server/src/uds/templates/uds/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@
</svg>
</div>
</uds-root>
<link rel="modulepreload" href="/uds/res/admin/chunk-2F3F2YC2.js?stamp=1731510528" integrity="sha384-VVOra5xy5Xg9fYkBmK9MLhX7vif/MexRAaLIDBsQ4ZlkF31s/U6uWWrj+LAnvX/q"><script src="/uds/res/admin/polyfills.js?stamp=1731510528" type="module" crossorigin="anonymous" integrity="sha384-TVRkn44wOGJBeCKWJBHWLvXubZ+Julj/yA0OoEFa3LgJHVHaPeeATX6NcjuNgsIA"></script><script src="/uds/res/admin/main.js?stamp=1731510528" type="module" crossorigin="anonymous" integrity="sha384-9qG6qxcP8yHMtsS0M4hGxgDGzNysCqOtP7J5vd6R0pdUbgYyu4LoaSIiIsUALPbN"></script></body>
<link rel="modulepreload" href="/uds/res/admin/chunk-2F3F2YC2.js?stamp=1731524874" integrity="sha384-VVOra5xy5Xg9fYkBmK9MLhX7vif/MexRAaLIDBsQ4ZlkF31s/U6uWWrj+LAnvX/q"><script src="/uds/res/admin/polyfills.js?stamp=1731524874" type="module" crossorigin="anonymous" integrity="sha384-TVRkn44wOGJBeCKWJBHWLvXubZ+Julj/yA0OoEFa3LgJHVHaPeeATX6NcjuNgsIA"></script><script src="/uds/res/admin/main.js?stamp=1731524874" type="module" crossorigin="anonymous" integrity="sha384-MlN4rQ8vJlHdGgHZgHXWK4Mk+8jxRDVsbHB754hBKa3LJ8lIJkQAydLWw8PP0ZFo"></script></body>

</html>

0 comments on commit 553bff6

Please sign in to comment.