Skip to content

Commit

Permalink
chg: [server] Return proper exit code in case of failure
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubOnderka committed Jul 2, 2023
1 parent 22f8faa commit 9e44f6b
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions misp_modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Core MISP expansion modules loader and web service
#
Expand Down Expand Up @@ -37,6 +36,8 @@
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

try:
from .modules import * # noqa
HAS_PACKAGE_MODULES = True
Expand Down Expand Up @@ -92,8 +93,8 @@ def load_helpers(helpersdir):

def load_package_helpers():
if not HAS_PACKAGE_HELPERS:
log.info('Unable to load MISP helpers from package.')
sys.exit()
log.error('Unable to load MISP helpers from package.')
sys.exit(1)
mhandlers = {}
helpers = []
for path, helper in sys.modules.items():
Expand Down Expand Up @@ -129,18 +130,18 @@ def load_modules(mod_dir):
try:
mhandlers[modulename] = importlib.import_module(os.path.basename(root) + '.' + modulename)
except Exception as e:
log.warning('MISP modules {0} failed due to {1}'.format(modulename, e))
log.warning('MISP module {0} failed due to {1}'.format(modulename, e))
continue
modules.append(modulename)
log.info('MISP modules {0} imported'.format(modulename))
log.info('MISP module {0} imported'.format(modulename))
mhandlers['type:' + modulename] = moduletype
return mhandlers, modules


def load_package_modules():
if not HAS_PACKAGE_MODULES:
log.info('Unable to load MISP modules from package.')
sys.exit()
log.error('Unable to load MISP modules from package.')
sys.exit(1)
mhandlers = {}
modules = []
for path, module in sys.modules.items():
Expand All @@ -149,7 +150,7 @@ def load_package_modules():
moduletype, modulename = r[0]
mhandlers[modulename] = module
modules.append(modulename)
log.info('MISP modules {0} imported'.format(modulename))
log.info('MISP module {0} imported'.format(modulename))
mhandlers['type:' + modulename] = moduletype
return mhandlers, modules

Expand Down Expand Up @@ -214,20 +215,22 @@ def _launch_from_current_dir():
return load_modules(modulesdir)


def main():
def main() -> int:
global mhandlers
global loaded_modules
signal.signal(signal.SIGINT, handle_signal)
signal.signal(signal.SIGTERM, handle_signal)
argParser = argparse.ArgumentParser(description='misp-modules server', formatter_class=argparse.RawTextHelpFormatter)
argParser.add_argument('-t', default=False, action='store_true', help='Test mode')
argParser.add_argument('-s', default=False, action='store_true', help='Run a system install (package installed via pip)')
argParser.add_argument('-d', default=False, action='store_true', help='Enable debugging')
argParser.add_argument('-p', default=6666, help='misp-modules TCP port (default 6666)')
argParser.add_argument('-l', default='localhost', help='misp-modules listen address (default localhost)')
argParser.add_argument('-m', default=[], action='append', help='Register a custom module')
argParser.add_argument('--devel', default=False, action='store_true', help='''Start in development mode, enable debug, start only the module(s) listed in -m.\nExample: -m misp_modules.modules.expansion.bgpranking''')
args = argParser.parse_args()

arg_parser = argparse.ArgumentParser(description='misp-modules server', formatter_class=argparse.RawTextHelpFormatter)
arg_parser.add_argument('-t', default=False, action='store_true', help='Test mode')
arg_parser.add_argument('-s', default=False, action='store_true', help='Run a system install (package installed via pip)')
arg_parser.add_argument('-d', default=False, action='store_true', help='Enable debugging')
arg_parser.add_argument('-p', default=6666, help='misp-modules TCP port (default 6666)')
arg_parser.add_argument('-l', default='localhost', help='misp-modules listen address (default localhost)')
arg_parser.add_argument('-m', default=[], action='append', help='Register a custom module')
arg_parser.add_argument('--devel', default=False, action='store_true', help='''Start in development mode, enable debug, start only the module(s) listed in -m.\nExample: -m misp_modules.modules.expansion.bgpranking''')
args = arg_parser.parse_args()

port = args.p
listen = args.l
if args.devel:
Expand Down Expand Up @@ -275,14 +278,14 @@ def main():
print("\nmisp-modules is still running as PID: {}\n".format(pid))
print("Please kill accordingly:")
print("sudo kill {}".format(pid))
sys.exit(-1)
return 1
print(e)
print("misp-modules might still be running.")

log.info('MISP modules server started on {0} port {1}'.format(listen, port))
if args.t:
log.info('MISP modules started in test-mode, quitting immediately.')
sys.exit()
return 0
try:
IOLoop.instance().start()
finally:
Expand Down

0 comments on commit 9e44f6b

Please sign in to comment.