diff --git a/molecule.py b/molecule.py index 0f105e5..03103de 100644 --- a/molecule.py +++ b/molecule.py @@ -25,7 +25,8 @@ sys.path.insert(0,'molecule/') sys.path.insert(0,'.') import molecule.cmdline from molecule.handlers import Runner -from molecule.utils import RUNNING_PIDS +from molecule.utils import RUNNING_PIDS, is_super_user +from molecule.i18n import _ def kill_pids(): for pid in RUNNING_PIDS: @@ -36,6 +37,17 @@ if not molecule_data_order: molecule.cmdline.print_help() raise SystemExit(1) +super_user = is_super_user() +for el in molecule_data_order: + spec_data = molecule_data.get(el) + if spec_data is None: + # wtf + continue + super_user_required = spec_data['__plugin__'].require_super_user() + if super_user_required and (not super_user): + sys.stderr.write("%s: %s\n" % (el, _("required super user access"),)) + raise SystemExit(1) + for el in molecule_data_order: my = Runner(el, molecule_data.get(el)) try: diff --git a/molecule/specs/skel.py b/molecule/specs/skel.py index 37ff8dd..e0c3188 100644 --- a/molecule/specs/skel.py +++ b/molecule/specs/skel.py @@ -152,6 +152,15 @@ class GenericSpec(GenericSpecFunctions): # Molecule Plugin factory support BASE_PLUGIN_API_VERSION = 0 + @staticmethod + def require_super_user(): + """ + Determine whether super user access is required in order to execute + the given GenericSpec subclass. Default: True. Please override if + you allow unprivileged user execution. + """ + return True + @staticmethod def execution_strategy(): """ diff --git a/molecule/utils.py b/molecule/utils.py index 47dd4ac..cb216fc 100644 --- a/molecule/utils.py +++ b/molecule/utils.py @@ -33,6 +33,12 @@ def get_year(): """ return time.strftime("%Y") +def is_super_user(): + """ + Return True if current process has uid = 0. + """ + return os.getuid() == 0 + def valid_exec_check(path): """ Determine whethern give path is valid executable (by running it).