Skip to content

Commit

Permalink
allow running cumin as a regular user
Browse files Browse the repository at this point in the history
Change-Id: I8fd3e941aaff3ffbe5eaf1446b8212ba2e7062a9
  • Loading branch information
anarcat committed May 2, 2019
1 parent 7b06f44 commit 26aaf06
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
13 changes: 6 additions & 7 deletions cumin/cli.py
Expand Up @@ -206,13 +206,12 @@ def parse_args(argv):


def get_running_user():
"""Ensure it's running as root and that the original user is detected and return it."""
if os.getenv('USER') != 'root':
raise cumin.CuminError('Insufficient privileges, run with sudo')
if os.getenv('SUDO_USER') in (None, 'root'):
raise cumin.CuminError('Unable to determine real user, logged in as root?')

return os.getenv('SUDO_USER')
"""Ensure that the original user is detected and return it."""
if os.getenv('USER') == 'root':
if os.getenv('SUDO_USER') in (None, 'root'):
raise cumin.CuminError('Unable to determine real user, logged in as root?')
return os.getenv('SUDO_USER')
return os.getenv('USER')


def setup_logging(filename, debug=False, trace=False):
Expand Down
11 changes: 5 additions & 6 deletions cumin/tests/unit/test_cli.py
Expand Up @@ -89,17 +89,16 @@ def test_target_batch_size_val_ko(value):


def test_get_running_user():
"""Unsufficient permissions or unknown user should raise CuminError and a proper user should be detected."""
env = {'USER': None, 'SUDO_USER': None}
with mock.patch('os.getenv', env.get):
with pytest.raises(CuminError, match='Insufficient privileges, run with sudo'):
cli.get_running_user()

"""Unknown user should raise CuminError and a proper user should be detected."""
env = {'USER': 'root', 'SUDO_USER': None}
with mock.patch('os.getenv', env.get):
with pytest.raises(CuminError, match='Unable to determine real user'):
cli.get_running_user()

env = {'USER': 'user', 'SUDO_USER': None}
with mock.patch('os.getenv', env.get):
assert cli.get_running_user() == 'user'

with mock.patch('os.getenv', _ENV.get):
assert cli.get_running_user() == 'user'

Expand Down

0 comments on commit 26aaf06

Please sign in to comment.