Skip to content

Commit 4b0e838

Browse files
authored
[MRG] Handle root user case more gracefully. Fixes #696 (#723)
[MRG] Handle root user case more gracefully. Fixes #696
2 parents 2cded9b + f80fb4b commit 4b0e838

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

repo2docker/__main__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,16 @@ def make_r2d(argv=None):
303303
r2d.user_id = args.user_id
304304
if args.user_name:
305305
r2d.user_name = args.user_name
306+
if r2d.user_id == 0 and not r2d.dry_run:
307+
print("Root as the primary user in the image is not permitted.")
308+
print(
309+
"The uid and the username of the user invoking repo2docker "
310+
"is used to create a mirror account in the image by default. "
311+
"To override that behavior pass --user-id <numeric_id> and "
312+
" --user-name <string> to repo2docker.\n"
313+
"Please see repo2docker --help for more details.\n"
314+
)
315+
sys.exit(1)
306316

307317
if args.build_memory_limit:
308318
# if the string only contains numerals we assume it should be an int

repo2docker/app.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
88
python -m repo2docker https://github.com/you/your-repo
99
"""
10-
import errno
1110
import json
1211
import sys
1312
import logging
@@ -670,17 +669,9 @@ def build(self):
670669

671670
if not self.dry_run:
672671
if self.user_id == 0:
673-
self.log.error(
674-
"Root as the primary user in the image is not permitted.\n"
672+
raise ValueError(
673+
"Root as the primary user in the image is not permitted."
675674
)
676-
self.log.info(
677-
"The uid and the username of the user invoking repo2docker "
678-
"is used to create a mirror account in the image by default. "
679-
"To override that behavior pass --user-id <numeric_id> and "
680-
" --user-name <string> to repo2docker.\n"
681-
"Please see repo2docker --help for more details.\n"
682-
)
683-
sys.exit(errno.EPERM)
684675

685676
build_args = {
686677
"NB_USER": self.user_name,

tests/unit/test_app.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,13 @@ def test_root_not_allowed():
110110
with TemporaryDirectory() as src, patch("os.geteuid") as geteuid:
111111
geteuid.return_value = 0
112112
argv = [src]
113-
app = make_r2d(argv)
114113
with pytest.raises(SystemExit) as exc:
114+
app = make_r2d(argv)
115+
assert exc.code == 1
116+
117+
with pytest.raises(ValueError):
118+
app = Repo2Docker(repo=src, run=False)
115119
app.build()
116-
assert exc.code == errno.EPERM
117120

118121
app = Repo2Docker(repo=src, user_id=1000, user_name="jovyan", run=False)
119122
app.initialize()

0 commit comments

Comments
 (0)