From f80fb4b39b22bb43287cd8cb84b9d150a6f3f5bb Mon Sep 17 00:00:00 2001 From: "Kacper Kowalik (Xarthisius)" Date: Fri, 28 Jun 2019 11:46:01 -0500 Subject: [PATCH] Handle root user case more gracefully. Fixes #696 If user_id is root, exit only from cmdline. Raise exception if r2d was invoked as a library. --- repo2docker/__main__.py | 10 ++++++++++ repo2docker/app.py | 13 ++----------- tests/unit/test_app.py | 7 +++++-- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/repo2docker/__main__.py b/repo2docker/__main__.py index ce7c8281..607e103e 100644 --- a/repo2docker/__main__.py +++ b/repo2docker/__main__.py @@ -303,6 +303,16 @@ def make_r2d(argv=None): r2d.user_id = args.user_id if args.user_name: r2d.user_name = args.user_name + if r2d.user_id == 0 and not r2d.dry_run: + print("Root as the primary user in the image is not permitted.") + print( + "The uid and the username of the user invoking repo2docker " + "is used to create a mirror account in the image by default. " + "To override that behavior pass --user-id and " + " --user-name to repo2docker.\n" + "Please see repo2docker --help for more details.\n" + ) + sys.exit(1) if args.build_memory_limit: # if the string only contains numerals we assume it should be an int diff --git a/repo2docker/app.py b/repo2docker/app.py index c09ee9f4..e3fc79ee 100644 --- a/repo2docker/app.py +++ b/repo2docker/app.py @@ -7,7 +7,6 @@ Usage: python -m repo2docker https://github.com/you/your-repo """ -import errno import json import sys import logging @@ -670,17 +669,9 @@ class Repo2Docker(Application): if not self.dry_run: if self.user_id == 0: - self.log.error( - "Root as the primary user in the image is not permitted.\n" + raise ValueError( + "Root as the primary user in the image is not permitted." ) - self.log.info( - "The uid and the username of the user invoking repo2docker " - "is used to create a mirror account in the image by default. " - "To override that behavior pass --user-id and " - " --user-name to repo2docker.\n" - "Please see repo2docker --help for more details.\n" - ) - sys.exit(errno.EPERM) build_args = { "NB_USER": self.user_name, diff --git a/tests/unit/test_app.py b/tests/unit/test_app.py index 747ed231..55845092 100644 --- a/tests/unit/test_app.py +++ b/tests/unit/test_app.py @@ -110,10 +110,13 @@ def test_root_not_allowed(): with TemporaryDirectory() as src, patch("os.geteuid") as geteuid: geteuid.return_value = 0 argv = [src] - app = make_r2d(argv) with pytest.raises(SystemExit) as exc: + app = make_r2d(argv) + assert exc.code == 1 + + with pytest.raises(ValueError): + app = Repo2Docker(repo=src, run=False) app.build() - assert exc.code == errno.EPERM app = Repo2Docker(repo=src, user_id=1000, user_name="jovyan", run=False) app.initialize()