|
14 | 14 | import importlib |
15 | 15 | from functools import partial |
16 | 16 |
|
17 | | -from .utils import print_err, map_path_as_posix, print_ok, decode_or_str, print_warn |
| 17 | +from .utils import get_user_environment, print_err, map_path_as_posix, print_ok, decode_or_str, print_warn |
18 | 18 | from .error import RefUtilsError, RefUtilsProcessTimeoutError, RefUtilsProcessError |
19 | 19 |
|
20 | 20 | _DEFAULT_DROP_UID = 9999 |
@@ -47,35 +47,6 @@ def ref_util_install_global_exception_hook() -> None: |
47 | 47 | hook = partial(ref_util_exception_hook, redact_traceback=False) |
48 | 48 | sys.excepthook = hook |
49 | 49 |
|
50 | | - |
51 | | -def get_user_env(last_cmd: Optional[Union[str, bytes]]) -> Dict[str, Union[str, bytes]]: |
52 | | - """ |
53 | | - The task tool (task-wrapper.c) dumps the user environment in the moment it is executed |
54 | | - to disk. This function retrives the dumped environment from the file and returns it. |
55 | | - This allows to restore the user's exact environment which is paramount for tasks that |
56 | | - require a stable stack layout. |
57 | | - Returns: |
58 | | - The mapping of all key value pairs of the user environment variables that where |
59 | | - defined during submission. |
60 | | - """ |
61 | | - ret: Dict[str, Union[str, bytes]] = {} |
62 | | - content = Path('/tmp/.user_environ').read_text() |
63 | | - lines = content.split('\x00') |
64 | | - for line in lines: |
65 | | - if line == '': |
66 | | - continue |
67 | | - |
68 | | - try: |
69 | | - k, v = line.split('=', 1) |
70 | | - except Exception as e: |
71 | | - print_err(f'Unexpected error while processing "{line}". Error: {e}.') |
72 | | - else: |
73 | | - ret[k] = v |
74 | | - |
75 | | - if last_cmd is not None: |
76 | | - ret['_'] = last_cmd |
77 | | - return ret |
78 | | - |
79 | 50 | # Hopefully safe, if not, please tell us, dont mess with the system. Thanks :) |
80 | 51 | class RestrictedUnpickler(pickle.Unpickler): |
81 | 52 | ALLOWED_MODULE_NAME = { |
@@ -169,7 +140,10 @@ def run(cmd_: List[Union[str, Path, bytes]], *args: str, **kwargs: Any) -> 'subp |
169 | 140 | # Restore the environment from the user as of the time she called `task ...`. |
170 | 141 | # NOTE: The stored environment contains user controlled input! |
171 | 142 | # Never restore the environment in a privileged context. |
172 | | - kwargs['env'] = get_user_env(cmd[0]) |
| 143 | + env = get_user_environment() |
| 144 | + # Set the last executed command variable ("_") to the correct value. |
| 145 | + env["_"] = cmd[0] |
| 146 | + kwargs['env'] = env |
173 | 147 |
|
174 | 148 | if 'timeout' not in kwargs: |
175 | 149 | kwargs['timeout'] = 10 |
@@ -206,13 +180,15 @@ def run_capture_output(*args: str, check_signal: bool = True, **kwargs: Any) -> |
206 | 180 | Wrapper of subprocess.run that redirects stderr to stdout and returns |
207 | 181 | (returncode, stdout). This methods raises the same exceptions as ref-utils |
208 | 182 | run() method. |
| 183 | + If `env` is not set, the user environment when she called `task check` is restored. |
209 | 184 | """ |
210 | 185 | p = run(*args, **kwargs, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check_signal=check_signal) |
211 | 186 | return p.returncode, p.stdout |
212 | 187 |
|
213 | 188 | def get_payload_from_executable(cmd_: List[Union[str, Path, bytes]], check: bool = True, check_signal: bool = True, verbose: bool = True, timeout: int = 10) -> Tuple[int, bytes]: |
214 | 189 | """ |
215 | 190 | Get the payload from a script/binary by executing it and returning the output. |
| 191 | + If `env` is not set, the user environment when she called `task check` is restored. |
216 | 192 | Args: |
217 | 193 | cmd: The command to execute. |
218 | 194 | check: Same as for subprocess.run. |
|
0 commit comments