Skip to content

Commit 9d08a21

Browse files
committed
Sanitizing the docker user in calls to exec_run()
1 parent 302fe68 commit 9d08a21

2 files changed

Lines changed: 34 additions & 39 deletions

File tree

tests/test_helpers.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,23 @@ def _install_WfCommons_on_container(container):
5050
tar_data = _make_tarfile_of_wfcommons()
5151
container.put_archive(target_path, tar_data)
5252
# Cleanup files that came from the host
53-
exit_code, output = container.exec_run("/bin/rm -rf /tmp/WfCommons/build/", user="root", stdout=True, stderr=True)
54-
exit_code, output = container.exec_run("/bin/rm -rf /tmp/WfCommons/*.egg-info/", user="root", stdout=True, stderr=True)
53+
exit_code, output = container.exec_run("sudo chown -R wfcommons:wfcommons /tmp/WfCommons", user="wfcommons", stdout=True, stderr=True)
54+
exit_code, output = container.exec_run("/bin/rm -rf /tmp/WfCommons/build/", user="wfcommons", stdout=True, stderr=True)
55+
exit_code, output = container.exec_run("/bin/rm -rf /tmp/WfCommons/*.egg-info/", user="wfcommons", stdout=True, stderr=True)
5556
# Clean up and force a rebuild of cpu-benchmark (because it may be compiled for the wrong architecture)
56-
exit_code, output = container.exec_run("/bin/rm -rf /tmp/WfCommons/bin/cpu-benchmark.o", user="root", stdout=True,
57+
exit_code, output = container.exec_run("/bin/rm -rf /tmp/WfCommons/bin/cpu-benchmark.o", user="wfcommons", stdout=True,
5758
stderr=True)
58-
exit_code, output = container.exec_run("/bin/rm -rf /tmp/WfCommons/bin/cpu-benchmark", user="root", stdout=True,
59+
exit_code, output = container.exec_run("/bin/rm -rf /tmp/WfCommons/bin/cpu-benchmark", user="wfcommons", stdout=True,
5960
stderr=True)
6061

6162
# Install WfCommons on the container (to install wfbench and cpu-benchmark really)
62-
exit_code, output = container.exec_run("python3 -m pip install . --break-system-packages",
63-
user="root",
63+
exit_code, output = container.exec_run("sudo python3 -m pip install . --break-system-packages",
64+
user="wfcommons",
6465
workdir="/tmp/WfCommons", stdout=True, stderr=True)
6566
if exit_code != 0:
6667
print(output.decode())
6768
raise RuntimeError("Failed to pip install WfCommons on the container")
6869

69-
# Copy the ~/.local directory to /home/wfcommons/, because the path
70-
# is /home/wfcommons (because this current Docker user may be unknown a priori)
71-
# This is hack, and requires that the /home/wfcommons directory be world-read-write-exec,
72-
# but who cares, these are containers!
73-
74-
# exit_code, output = container.exec_run(
75-
# ["sh", "-c", "whoami"],
76-
# stdout=True, stderr=True)
77-
# print(f"WHOAMI: {output.decode()}")
78-
# exit_code, output = container.exec_run(
79-
# ["sh", "-c", "ls /home/wfcommons/.local"],
80-
# stdout=True, stderr=True)
81-
# print(f"LS /home/wfcommons/.local : {output.decode()}")
82-
# exit_code, output = container.exec_run(
83-
# ["sh", "-c", "cp -r /.local /home/wfcommons/"],
84-
# stdout=True, stderr=True)
85-
8670

8771
def _start_docker_container(backend, mounted_dir, working_dir, bin_dir, command=None):
8872
if command is None:
@@ -105,6 +89,7 @@ def _start_docker_container(backend, mounted_dir, working_dir, bin_dir, command=
10589
command=command,
10690
volumes={mounted_dir: {'bind': mounted_dir, 'mode': 'rw'}},
10791
working_dir=working_dir,
92+
user="wfcommons",
10893
tty=True,
10994
detach=True
11095
)
@@ -116,19 +101,22 @@ def _start_docker_container(backend, mounted_dir, working_dir, bin_dir, command=
116101
if bin_dir:
117102
sys.stderr.write(f"[{backend}] Copying wfbench and cpu-benchmark...\n")
118103
exit_code, output = container.exec_run(["sh", "-c", "sudo cp -f `which wfbench` " + bin_dir],
104+
user="wfcommons",
119105
stdout=True, stderr=True)
120106
if exit_code != 0:
121107
raise RuntimeError("Failed to copy wfbench script to the bin directory")
122108

123109
exit_code, output = container.exec_run(["sh", "-c", "sudo cp -f `which cpu-benchmark` " + bin_dir],
110+
user="wfcommons",
124111
stdout=True, stderr=True)
125112
if exit_code != 0:
126113
raise RuntimeError("Failed to copy cpu-benchmark executable to the bin directory")
127114
else:
128115
sys.stderr.write(f"[{backend}] Not Copying wfbench and cpu-benchmark...\n")
129116

130117
# Change file permissions
131-
exit_code, output = container.exec_run(["sh", "-c", "sudo chown -R wfcommons:wfcommons "],
118+
exit_code, output = container.exec_run(["sh", "-c", "sudo chown -R wfcommons:wfcommons ."],
119+
user="wfcommons",
132120
stdout=True, stderr=True)
133121

134122

tests/translators_loggers/test_translators_loggers.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def _additional_setup_taskvine(container):
6565
# Create the poncho package
6666
exit_code, output = container.exec_run(cmd=["bash", "-c",
6767
"source ~/conda/etc/profile.d/conda.sh && conda activate && poncho_package_create taskvine_poncho.json taskvine_poncho.tar.gz"],
68+
user="wfcommons",
6869
stdout=True, stderr=True)
6970
if exit_code != 0:
7071
raise Exception("Failed to setup TaskVine: cannot create poncho package")
@@ -79,25 +80,27 @@ def _additional_setup_pegasus(container):
7980
# Start Condor
8081
exit_code, output = container.exec_run(cmd=["bash", "-c",
8182
"bash /home/wfcommons/start_condor.sh"],
83+
user="wfcommons",
8284
stdout=True, stderr=True)
8385
if exit_code != 0:
8486
raise Exception("Failed to setup Pegasus: cannot start HTCondor")
8587
# Run pegasus script
8688
exit_code, output = container.exec_run(cmd=["bash", "-c",
8789
"python3 ./pegasus_workflow.py"],
90+
user="wfcommons",
8891
stdout=True, stderr=True)
8992
if exit_code != 0:
9093
raise Exception("Failed to setup Pegasus: error while running the pegasus_workflow.py script")
9194

9295
def _additional_setup_swiftt(container):
9396
# Start a redis server in the background
9497
exit_code, output = container.exec_run(
95-
cmd=["bash", "-c", "redis-server"], detach=True, stdout=True, stderr=True)
98+
cmd=["bash", "-c", "redis-server"], user="wfcommons", detach=True, stdout=True, stderr=True)
9699
# Note that exit_code will always be None because of detach=True.
97100

98101
# Check that the redis-server is up
99102
exit_code, output = container.exec_run(
100-
cmd=["bash", "-c", "redis-cli ping"], stdout=True, stderr=True)
103+
cmd=["bash", "-c", "redis-cli ping"], user="wfcommons", stdout=True, stderr=True)
101104
if output.decode().strip() != 'PONG':
102105
raise Exception("Failed to start redis-server...")
103106

@@ -120,24 +123,24 @@ def _additional_setup_swiftt(container):
120123
#############################################################################
121124

122125
def run_workflow_dask(container, num_tasks, str_dirpath):
123-
exit_code, output = container.exec_run("python ./dask_workflow.py", stdout=True, stderr=True)
126+
exit_code, output = container.exec_run("python ./dask_workflow.py", user="wfcommons", stdout=True, stderr=True)
124127
# Check sanity
125128
assert (exit_code == 0)
126129
assert (output.decode().count("completed!") == num_tasks)
127130
# TODO: Look at the (I think) generated run.json file on the container?
128131

129132
def run_workflow_parsl(container, num_tasks, str_dirpath):
130-
exit_code, output = container.exec_run("python ./parsl_workflow.py", stdout=True, stderr=True)
131-
ignored, output = container.exec_run(f"cat {str_dirpath}/runinfo/000/parsl.log", stdout=True, stderr=True)
133+
exit_code, output = container.exec_run("python ./parsl_workflow.py", user="wfcommons", stdout=True, stderr=True)
134+
ignored, output = container.exec_run(f"cat {str_dirpath}/runinfo/000/parsl.log", user="wfcommons", stdout=True, stderr=True)
132135
# Check sanity
133136
assert (exit_code == 0)
134137
assert ("completed" in output.decode())
135138
assert (output.decode().count("_complete_task") == num_tasks)
136139

137140
def run_workflow_nextflow(container, num_tasks, str_dirpath):
138141
# Run the workflow!
139-
exit_code, output = container.exec_run(f"nextflow run ./workflow.nf --pwd .", stdout=True, stderr=True)
140-
ignored, task_exit_codes = container.exec_run("find . -name .exitcode -exec cat {} \;", stdout=True, stderr=True)
142+
exit_code, output = container.exec_run(f"nextflow run ./workflow.nf --pwd .", user="wfcommons", stdout=True, stderr=True)
143+
ignored, task_exit_codes = container.exec_run("find . -name .exitcode -exec cat {} \;", user="wfcommons", stdout=True, stderr=True)
141144
# Check sanity
142145
assert (exit_code == 0)
143146
assert (task_exit_codes.decode() == num_tasks * "0")
@@ -146,29 +149,30 @@ def run_workflow_airflow(container, num_tasks, str_dirpath):
146149
# Run the workflow! (use a specific working directory)
147150
# TODO: Remove the hardcoded Blast-Benchmark as it's ugly
148151
exit_code, output = container.exec_run(cmd=["sh", "-c", "cd /home/wfcommons/ && sudo /bin/bash /run_a_workflow.sh Blast-Benchmark"],
149-
stdout=True,
150-
stderr=True)
152+
user="wfcommons", stdout=True, stderr=True)
151153
# Check sanity
152154
assert (exit_code == 0)
153155
assert (output.decode().count("completed") == num_tasks * 2)
154156

155157
def run_workflow_bash(container, num_tasks, str_dirpath):
156158
# Run the workflow!
157-
exit_code, output = container.exec_run(cmd="/bin/bash ./run_workflow.sh", stdout=True, stderr=True)
159+
exit_code, output = container.exec_run(cmd="/bin/bash ./run_workflow.sh", user="wfcommons", stdout=True, stderr=True)
158160
# Check sanity
159161
assert (exit_code == 0)
160162
assert (output.decode().count("completed") == num_tasks)
161163

162164
def run_workflow_taskvine(container, num_tasks, str_dirpath):
163165
# Run the workflow!
164-
exit_code, output = container.exec_run(cmd=["bash", "-c", "source ~/conda/etc/profile.d/conda.sh && conda activate && python3 ./taskvine_workflow.py"], stdout=True, stderr=True)
166+
exit_code, output = container.exec_run(cmd=["bash", "-c", "source ~/conda/etc/profile.d/conda.sh && conda activate && python3 ./taskvine_workflow.py"],
167+
user="wfcommons", stdout=True, stderr=True)
165168
# Check sanity
166169
assert (exit_code == 0)
167170
assert (output.decode().count("completed") == num_tasks)
168171

169172
def run_workflow_makeflow(container, num_tasks, str_dirpath):
170173
# Run the workflow (with full logging)
171-
exit_code, output = container.exec_run(cmd=["bash", "-c", "source ~/conda/etc/profile.d/conda.sh && conda activate && makeflow --log-verbose --monitor=./monitor_data/ ./workflow.makeflow"], stdout=True, stderr=True)
174+
exit_code, output = container.exec_run(cmd=["bash", "-c", "source ~/conda/etc/profile.d/conda.sh && conda activate && makeflow --log-verbose --monitor=./monitor_data/ ./workflow.makeflow"],
175+
user="wfcommons", stdout=True, stderr=True)
172176
# Check sanity
173177
assert (exit_code == 0)
174178
num_completed_jobs = len(re.findall(r'job \d+ completed', output.decode()))
@@ -177,7 +181,8 @@ def run_workflow_makeflow(container, num_tasks, str_dirpath):
177181
def run_workflow_cwl(container, num_tasks, str_dirpath):
178182
# Run the workflow!
179183
# Note that the input file is hardcoded and Blast-specific
180-
exit_code, output = container.exec_run(cmd="cwltool ./main.cwl --split_fasta_00000001_input ./data/workflow_infile_0001 ", stdout=True, stderr=True)
184+
exit_code, output = container.exec_run(cmd="cwltool ./main.cwl --split_fasta_00000001_input ./data/workflow_infile_0001 ",
185+
user="wfcommons", stdout=True, stderr=True)
181186
# Check sanity
182187
assert (exit_code == 0)
183188
# this below is ugly (the 3 is for "workflow", "compile_output_files" and "compile_log_files",
@@ -186,14 +191,16 @@ def run_workflow_cwl(container, num_tasks, str_dirpath):
186191

187192
def run_workflow_pegasus(container, num_tasks, str_dirpath):
188193
# Run the workflow!
189-
exit_code, output = container.exec_run(cmd="bash /home/wfcommons/run_workflow.sh", stdout=True, stderr=True)
194+
exit_code, output = container.exec_run(cmd="bash /home/wfcommons/run_workflow.sh",
195+
user="wfcommons", stdout=True, stderr=True)
190196
# Check sanity
191197
assert(exit_code == 0)
192198
assert("success" in output.decode())
193199

194200
def run_workflow_swiftt(container, num_tasks, str_dirpath):
195201
# Run the workflow!
196-
exit_code, output = container.exec_run(cmd="swift-t workflow.swift", stdout=True, stderr=True)
202+
exit_code, output = container.exec_run(cmd="swift-t workflow.swift",
203+
user="wfcommons", stdout=True, stderr=True)
197204
# sys.stderr.write(output.decode())
198205
# Check sanity
199206
assert(exit_code == 0)

0 commit comments

Comments
 (0)