From 3408ac8f88200c40b94581804b5f326fdcc46229 Mon Sep 17 00:00:00 2001 From: Chris Church Date: Wed, 13 Aug 2014 01:03:52 -0400 Subject: [PATCH] Display error message if awx-manage/tower-manage are run as a user other than root or awx. --- awx/__init__.py | 10 ++++++++++ awx/settings/production.py | 12 +++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/awx/__init__.py b/awx/__init__.py index d62e248add..a85c982c34 100644 --- a/awx/__init__.py +++ b/awx/__init__.py @@ -74,8 +74,18 @@ def manage(): # Prepare the AWX environment. prepare_env() # Now run the command (or display the version). + from django.conf import settings from django.core.management import execute_from_command_line if len(sys.argv) >= 2 and sys.argv[1] in ('version', '--version'): # pragma: no cover sys.stdout.write('%s\n' % __version__) + # If running as a user without permission to read settings, display an + # error message. Allow --help to still work. + elif getattr(settings, 'MUST_BE_ROOT_OR_AWX', False): + if len(sys.argv) == 1 or len(sys.argv) >= 2 and sys.argv[1] in ('-h', '--help'): + execute_from_command_line(sys.argv) + sys.stdout.write('\n') + prog = os.path.basename(sys.argv[0]) + sys.stdout.write('%s must be run as root or awx.\n' % prog) + sys.exit(1) else: execute_from_command_line(sys.argv) diff --git a/awx/settings/production.py b/awx/settings/production.py index 66ea2fb8c4..ca3200ec28 100644 --- a/awx/settings/production.py +++ b/awx/settings/production.py @@ -4,6 +4,7 @@ # Production settings for AWX project. # Python +import errno import sys import traceback @@ -74,13 +75,18 @@ try: except ImportError: traceback.print_exc() sys.exit(1) -except IOError: +except IOError, e: from django.core.exceptions import ImproperlyConfigured included_file = locals().get('__included_file__', '') if (not included_file or included_file == settings_file) and not os.path.exists(settings_file): - if 'AWX_SETTINGS_FILE' not in os.environ: + if e.errno == errno.EACCES: + MUST_BE_ROOT_OR_AWX = True + elif 'AWX_SETTINGS_FILE' not in os.environ: msg = 'No AWX configuration found at %s.' % settings_file msg += '\nDefine the AWX_SETTINGS_FILE environment variable to ' msg += 'specify an alternate path.' raise ImproperlyConfigured(msg) - raise + else: + raise + else: + raise