Skip to content

Commit 0c8ce47

Browse files
committed
Try to grab real ssh username from system if it not used for connection directly
1 parent 8712742 commit 0c8ce47

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

exec_helpers/_ssh_client_base.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
import concurrent.futures
2323
import copy
2424
import datetime
25+
import getpass
2526
import logging
27+
import os
2628
import shlex
2729
import socket
2830
import stat
31+
import sys
2932
import time
3033
import typing
3134
import warnings
@@ -292,9 +295,11 @@ def __init__(
292295

293296
# Init super with host and real port and username
294297
mod_name = "exec_helpers" if self.__module__.startswith("exec_helpers") else self.__module__
298+
log_username: typing.Optional[str] = self.__get_user_for_log(real_auth)
299+
295300
super(SSHClientBase, self).__init__(
296301
logger=logging.getLogger(f"{mod_name}.{self.__class__.__name__}").getChild(
297-
f"({real_auth.username}@{host}:{self.__port})"
302+
f"({log_username}@{host}:{self.__port})"
298303
)
299304
)
300305

@@ -310,6 +315,20 @@ def __init__(
310315

311316
self.__connect()
312317

318+
@staticmethod
319+
def __get_user_for_log(real_auth: ssh_auth.SSHAuth) -> typing.Optional[str]: # pragma: no cover
320+
if real_auth.username is not None:
321+
return real_auth.username
322+
# noinspection PyBroadException
323+
try:
324+
if sys.platform != "win32":
325+
import pwd # pylint: disable=import-outside-toplevel
326+
327+
return pwd.getpwuid(os.getuid()).pw_name # Correct for not windows only
328+
return getpass.getuser()
329+
except Exception:
330+
return None
331+
313332
def __rebuild_ssh_config(self) -> None:
314333
"""Rebuild main ssh config from available information."""
315334
self.__ssh_config[self.hostname] = self.__ssh_config[self.hostname].overridden_by(
@@ -514,6 +533,7 @@ def __del__(self) -> None:
514533

515534
def __enter__(self) -> "SSHClientBase": # pylint: disable=useless-super-delegation
516535
"""Get context manager."""
536+
# noinspection PyTypeChecker
517537
return super().__enter__()
518538

519539
def __exit__(self, exc_type: typing.Any, exc_val: typing.Any, exc_tb: typing.Any) -> None:

exec_helpers/async_api/api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ def __init__(self, conn: "ExecHelper", path: typing.Optional[typing.Union[str, p
5454
super().__init__(conn=conn, path=path)
5555

5656
async def __aenter__(self) -> None:
57+
# noinspection PyUnresolvedReferences
5758
await self._conn.__aenter__()
5859
self._chroot_status = self._conn._chroot_path
5960
self._conn._chroot_path = self._path
6061

6162
async def __aexit__(self, exc_type: typing.Any, exc_val: typing.Any, exc_tb: typing.Any) -> None:
6263
self._conn._chroot_path = self._chroot_status
64+
# noinspection PyUnresolvedReferences
6365
await self._conn.__aexit__(exc_type=exc_type, exc_val=exc_val, exc_tb=exc_tb)
6466

6567

exec_helpers/async_api/subprocess_runner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ def __init__(
102102

103103
async def __aenter__(self) -> "Subprocess":
104104
"""Async context manager."""
105+
# noinspection PyTypeChecker
105106
return await super().__aenter__()
106107

107108
def __enter__(self) -> "Subprocess": # pylint: disable=useless-super-delegation
108109
"""Get context manager."""
110+
# noinspection PyTypeChecker
109111
return super().__enter__()
110112

111113
async def _exec_command( # type: ignore

exec_helpers/subprocess_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def __init__(self, log_mask_re: typing.Optional[str] = None,) -> None:
9999

100100
def __enter__(self) -> "Subprocess": # pylint: disable=useless-super-delegation
101101
"""Get context manager."""
102+
# noinspection PyTypeChecker
102103
return super().__enter__()
103104

104105
def _exec_command( # type: ignore

0 commit comments

Comments
 (0)