Skip to content

Commit 892c703

Browse files
committed
Log the username of CLI users
This passes the discovered CLI user, if available, and adds them with a reference to who it might be. Uses getlogin by default, which grabs the user based on the controlling terminal and if that fails uses getuser which pulls from environment variables. This passed couched in the terms of "possibly on behalf of" as this is data from the client and so is not reliable.
1 parent 827a698 commit 892c703

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

cylc/flow/network/client.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
abstractmethod,
2121
)
2222
import asyncio
23+
import getpass
2324
import os
2425
from shutil import which
2526
import socket
@@ -366,6 +367,16 @@ def get_header(self) -> dict:
366367

367368
if cmd.startswith(cylc_bin_dir):
368369
cmd = cmd.replace(cylc_bin_dir, '')
370+
try:
371+
behalf_of = f" (possibly on behalf of {os.getlogin()})"
372+
except OSError:
373+
behalf_of = None
374+
if not behalf_of:
375+
try:
376+
behalf_of = f" (possibly on behalf of {getpass.getuser()})"
377+
except OSError:
378+
behalf_of = ''
379+
369380
return {
370381
'meta': {
371382
'prog': cmd,
@@ -374,6 +385,7 @@ def get_header(self) -> dict:
374385
os.getenv(
375386
"CLIENT_COMMS_METH",
376387
default=CommsMeth.ZMQ.value
377-
)
388+
),
389+
'behalf_of': behalf_of
378390
}
379391
}

cylc/flow/network/resolvers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,13 +723,14 @@ async def _mutation_mapper(
723723
Others go to the scheduler command queue.
724724
725725
"""
726+
behalf_of = meta.get('behalf_of', '')
726727
user = meta.get('auth_user', self.schd.owner)
727728
if user == self.schd.owner:
728729
log_user = f" from {self.schd.owner}"
729730
else:
730731
log_user = f" from {user}"
731732

732-
log1 = f'Command "{command}" received{log_user}.'
733+
log1 = f'Command "{command}" received{log_user}{behalf_of}.'
733734
log2 = (
734735
f"{command}("
735736
+ ", ".join(

tests/integration/network/test_resolvers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ async def test_command_logging(mock_flow, caplog, log_filter):
251251
await mock_flow.resolvers._mutation_mapper("put_messages", kwargs, meta)
252252
assert log_filter(contains='Command "put_messages" received from Dr Spock')
253253

254+
meta["auth_user"] = "Dr Spock"
255+
meta["behalf_of"] = "Mr Nimoy"
256+
await mock_flow.resolvers._mutation_mapper("put_messages", kwargs, meta)
257+
assert log_filter(
258+
contains='received from Dr Spock (possibly on behalf of Mr Nimoy)')
259+
254260

255261
async def test_command_validation_failure(
256262
mock_flow,

0 commit comments

Comments
 (0)