Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/183'
Browse files Browse the repository at this point in the history
* origin/pr/183:
  Make --force-root parameter optionally visible again
  • Loading branch information
marmarek committed Sep 11, 2021
2 parents f24b18d + 6c77cc4 commit baf5037
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions qubesadmin/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ class QubesArgumentParser(argparse.ArgumentParser):
* '*' consumes zero or more arguments (and produces a list)
* '+' consumes one or more arguments (and produces a list)
:param show_forceroot: don't hide --force-root parameter, prevent running
as root unless it is given
*kwargs* are passed to :py:class:`argparser.ArgumentParser`.
Currenty supported options:
Expand All @@ -337,7 +340,7 @@ class QubesArgumentParser(argparse.ArgumentParser):
``--verbose`` and ``--quiet``
'''

def __init__(self, vmname_nargs=None, **kwargs):
def __init__(self, vmname_nargs=None, show_forceroot=False, **kwargs):

super().__init__(add_help=False, **kwargs)

Expand All @@ -349,8 +352,15 @@ def __init__(self, vmname_nargs=None, **kwargs):
self.add_argument('--quiet', '-q', action='count',
help='decrease verbosity')

self.add_argument('--force-root', action='store_true',
default=False, help=argparse.SUPPRESS)
if show_forceroot:
self.add_argument(
'--force-root', action='store_true',
default=False,
help="Force running the tool even if called as root")
else:
self.add_argument('--force-root', action='store_true',
default=False, help=argparse.SUPPRESS)
self._complain_if_root = show_forceroot

self.add_argument('--help', '-h', action=SubParsersHelpAction,
help='show this help message and exit')
Expand All @@ -372,6 +382,11 @@ def parse_args(self, *args, **kwargs):
app = kwargs.pop('app', None)
namespace = super().parse_args(*args, **kwargs)

if self._complain_if_root and \
os.getuid() == 0 and \
not namespace.force_root:
self.error('refusing to run as root; add --force-root to override')

self.set_qubes_verbosity(namespace)
if app is not None:
namespace.app = app
Expand Down

0 comments on commit baf5037

Please sign in to comment.