Commit 6795119f authored by Marco Mariani's avatar Marco Mariani

Check that 'slapos node' commands are run as root.

parent fa924818
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import logging import logging
from slapos.cli.command import must_be_root
from slapos.cli.config import ConfigCommand from slapos.cli.config import ConfigCommand
from slapos.bang import do_bang from slapos.bang import do_bang
...@@ -19,6 +20,7 @@ class BangCommand(ConfigCommand): ...@@ -19,6 +20,7 @@ class BangCommand(ConfigCommand):
help='Message for bang') help='Message for bang')
return ap return ap
@must_be_root
def take_action(self, args): def take_action(self, args):
configp = self.fetch_config(args) configp = self.fetch_config(args)
do_bang(configp, args.message) do_bang(configp, args.message)
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import argparse import argparse
import functools
import os
import sys
import cliff import cliff
class Command(cliff.command.Command): class Command(cliff.command.Command):
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description=self.get_description(), description=self.get_description(),
...@@ -14,3 +18,12 @@ class Command(cliff.command.Command): ...@@ -14,3 +18,12 @@ class Command(cliff.command.Command):
) )
return parser return parser
def must_be_root(func):
@functools.wraps(func)
def func(self, *args, **kw):
if os.getuid() != 0:
self.app.log.error('This slapos command must be run as root.')
sys.exit(5)
return func
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import logging import logging
import sys import sys
from slapos.cli.command import must_be_root
from slapos.cli.config import ConfigCommand from slapos.cli.config import ConfigCommand
from slapos.format import do_format, FormatConfig, tracing_monkeypatch, UsageError from slapos.format import do_format, FormatConfig, tracing_monkeypatch, UsageError
...@@ -55,6 +56,7 @@ class FormatCommand(ConfigCommand): ...@@ -55,6 +56,7 @@ class FormatCommand(ConfigCommand):
return ap return ap
@must_be_root
def take_action(self, args): def take_action(self, args):
configp = self.fetch_config(args) configp = self.fetch_config(args)
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
import logging import logging
import sys import sys
from slapos.cli.command import Command from slapos.cli.command import Command, must_be_root
from slapos.register.register import do_register, RegisterConfig from slapos.register.register import do_register, RegisterConfig
...@@ -66,6 +66,7 @@ class RegisterCommand(Command): ...@@ -66,6 +66,7 @@ class RegisterCommand(Command):
return ap return ap
@must_be_root
def take_action(self, args): def take_action(self, args):
try: try:
conf = RegisterConfig(logger=self.log) conf = RegisterConfig(logger=self.log)
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import logging import logging
from slapos.cli.command import must_be_root
from slapos.cli.config import ConfigCommand from slapos.cli.config import ConfigCommand
from slapos.grid.utils import setRunning, setFinished from slapos.grid.utils import setRunning, setFinished
...@@ -57,6 +58,7 @@ class SlapgridCommand(ConfigCommand): ...@@ -57,6 +58,7 @@ class SlapgridCommand(ConfigCommand):
help='Launch slapgrid without delay. Default behavior.') help='Launch slapgrid without delay. Default behavior.')
return ap return ap
@must_be_root
def take_action(self, args): def take_action(self, args):
configp = self.fetch_config(args) configp = self.fetch_config(args)
options = merged_options(args, configp) options = merged_options(args, configp)
......
...@@ -4,6 +4,7 @@ import argparse ...@@ -4,6 +4,7 @@ import argparse
import logging import logging
import os import os
from slapos.cli.command import must_be_root
from slapos.cli.config import ConfigCommand from slapos.cli.config import ConfigCommand
from slapos.grid.svcbackend import launchSupervisord from slapos.grid.svcbackend import launchSupervisord
...@@ -22,6 +23,7 @@ class SupervisorctlCommand(ConfigCommand): ...@@ -22,6 +23,7 @@ class SupervisorctlCommand(ConfigCommand):
help='parameters passed to supervisorctl') help='parameters passed to supervisorctl')
return ap return ap
@must_be_root
def take_action(self, args): def take_action(self, args):
configp = self.fetch_config(args) configp = self.fetch_config(args)
instance_root = configp.get('slapos', 'instance_root') instance_root = configp.get('slapos', 'instance_root')
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import logging import logging
import os import os
from slapos.cli.command import must_be_root
from slapos.cli.config import ConfigCommand from slapos.cli.config import ConfigCommand
from slapos.grid.svcbackend import launchSupervisord from slapos.grid.svcbackend import launchSupervisord
...@@ -12,6 +13,7 @@ class SupervisordCommand(ConfigCommand): ...@@ -12,6 +13,7 @@ class SupervisordCommand(ConfigCommand):
log = logging.getLogger('supervisord') log = logging.getLogger('supervisord')
@must_be_root
def take_action(self, args): def take_action(self, args):
configp = self.fetch_config(args) configp = self.fetch_config(args)
instance_root = configp.get('slapos', 'instance_root') instance_root = configp.get('slapos', 'instance_root')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment