Keep the rule for which commands reach run-heavy's docker beside that docker in capped_docker.py
This commit is contained in:
+3
-10
@@ -5,8 +5,9 @@ import typing
|
||||
|
||||
from round_titles import CAP, lacks_counts, RELEASE_REVIEW_CAP
|
||||
from git_command import commits
|
||||
from run_heavy.capped_docker import finds_this_docker
|
||||
from rule_hook.heavy_command import heavy
|
||||
from shell_command import launched, Simple, tool_name
|
||||
from shell_command import launched, Simple
|
||||
from review_folder import REPORTS
|
||||
from docker_command import unlimited
|
||||
|
||||
@@ -162,15 +163,7 @@ def unlimited_container(words: list[str]) -> bool:
|
||||
docker = launched(words, {"docker"})
|
||||
if docker is None or not unlimited(docker.arguments):
|
||||
return False
|
||||
# On Linux, run-heavy's docker adds the limits a container lacks, where PATH finds it: docker named bare, with PATH left as run-heavy set it.
|
||||
before = words[:len(words) - len(docker.arguments) - 1]
|
||||
capped = "run-heavy" in docker.launchers and words[len(before)] == "docker" and not any(resets_path(word) for word in before)
|
||||
return not capped
|
||||
|
||||
|
||||
def resets_path(word: str) -> bool:
|
||||
"""Whether a word before docker drops the PATH run-heavy set: a PATH= of its own, env's -i or -u, or sudo or doas, which reset it."""
|
||||
return word.startswith(("PATH=", "--unset")) or word in ("-i", "--ignore-environment", "-u") or tool_name(word) in ("doas", "sudo")
|
||||
return not ("run-heavy" in docker.launchers and finds_this_docker(docker.word, docker.before))
|
||||
|
||||
|
||||
def file_breaches(path: str, before: str, added: str, after: str) -> list[Breach]:
|
||||
|
||||
@@ -6,6 +6,7 @@ import sys
|
||||
import typing
|
||||
|
||||
from docker_command import capped
|
||||
from shell_command import tool_name
|
||||
|
||||
# The folder run-heavy puts first on its command's PATH, holding the docker that runs main.
|
||||
PATH_ENTRY = pathlib.Path(__file__).resolve().parent / "path_entry"
|
||||
@@ -33,3 +34,13 @@ def main(argv: list[str], env: typing.Mapping[str, str]) -> int:
|
||||
def without_path_entry(path: str) -> str:
|
||||
"""path less PATH_ENTRY, so the real docker is the one found."""
|
||||
return os.pathsep.join(entry for entry in path.split(os.pathsep) if pathlib.Path(entry).resolve() != PATH_ENTRY)
|
||||
|
||||
|
||||
def finds_this_docker(word: str, before: list[str]) -> bool:
|
||||
"""Whether a command that runs under run-heavy reaches this docker, given the word that names docker and the words before it: docker named bare, since a path names another, and no word before it resets the PATH that run-heavy set. Only where run-heavy knows the memory, as on Linux."""
|
||||
return word == "docker" and not any(resets_path(earlier) for earlier in before)
|
||||
|
||||
|
||||
def resets_path(word: str) -> bool:
|
||||
"""Whether a word before docker drops the PATH run-heavy set: a PATH= of its own, env's -i or -u, or sudo or doas, which reset it."""
|
||||
return word.startswith(("PATH=", "--unset")) or word in ("-i", "--ignore-environment", "-u") or tool_name(word) in ("doas", "sudo")
|
||||
|
||||
@@ -9,6 +9,7 @@ import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from run_heavy.capped_docker import finds_this_docker
|
||||
from run_heavy import main
|
||||
from machine_load.memory import read_memory
|
||||
from machine_load.slots import slot_count, usable_cpus
|
||||
@@ -141,6 +142,14 @@ class RunHeavy(unittest.TestCase):
|
||||
self.assertEqual(helped.returncode, 0)
|
||||
self.assertIn("usage: run-heavy <command>", helped.stdout)
|
||||
|
||||
def test_the_capped_docker_is_found_only_by_docker_named_bare_with_no_word_before_it_resetting_path(self):
|
||||
for word, before in [("docker", []), ("docker", ["run-heavy"]), ("docker", ["run-heavy", "env", "X=1"]), ("docker", ["run-heavy", "nice", "-n", "19"])]:
|
||||
with self.subTest(word=word, before=before):
|
||||
self.assertTrue(finds_this_docker(word, before))
|
||||
for word, before in [("/usr/bin/docker", []), ("docker", ["env", "PATH=/usr/bin"]), ("docker", ["env", "-i"]), ("docker", ["env", "-u", "PATH"]), ("docker", ["env", "--unset=PATH"]), ("docker", ["sudo"]), ("docker", ["/usr/bin/doas"])]:
|
||||
with self.subTest(word=word, before=before):
|
||||
self.assertFalse(finds_this_docker(word, before))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -18,9 +18,11 @@ PIECE = re.compile(r"""'[^']*'|"(?:\\.|[^"\\])*"|\\.|(?<![^\s;&|()])#[^\n]*""",
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Launch:
|
||||
"""A tool a simple command runs: the launchers it runs through, such as nice and run-heavy, and the words it is given."""
|
||||
"""A tool a simple command runs: the launchers it runs through, such as nice and run-heavy, the word that names the tool as written, the words before that word and the words it is given."""
|
||||
launchers: frozenset[str]
|
||||
tool: str
|
||||
word: str
|
||||
before: list[str]
|
||||
arguments: list[str]
|
||||
|
||||
|
||||
@@ -107,4 +109,4 @@ def launched(words: list[str], tools: typing.Collection[str]) -> Launch | None:
|
||||
at = next((i for i, name in enumerate(reachable) if name in tools), None)
|
||||
if at is None:
|
||||
return None
|
||||
return Launch(frozenset(LAUNCHERS.intersection(names[:at])), names[at], words[at + 1:])
|
||||
return Launch(frozenset(LAUNCHERS.intersection(names[:at])), names[at], words[at], words[:at], words[at + 1:])
|
||||
|
||||
Reference in New Issue
Block a user