Skip to content

Commit

Permalink
Merge pull request #201 from mitre/feature/config-refactor
Browse files Browse the repository at this point in the history
Feature/config refactor
  • Loading branch information
clenk authored Oct 8, 2019
2 parents 9046b92 + 8dc9732 commit a33f818
Show file tree
Hide file tree
Showing 35 changed files with 855 additions and 788 deletions.
1 change: 1 addition & 0 deletions docker_utils/api_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ password =
vhost = /
flush_every = 100
flush_interval = 10
tz = US/Eastern

[Database]
db_type = sqlite
Expand Down
6 changes: 3 additions & 3 deletions multiscanner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from .config import ( # noqa F401
PY3, MS_WD, CONFIG, MODULESDIR
MSConfigParser, MS_WD, PY3, config_init, update_ms_config, update_ms_config_file
)

from .ms import ( # noqa F401
config_init, multiscan, parse_reports, _ModuleInterface,
_GlobalModuleInterface, _Thread, _run_module, _main
multiscan, parse_reports, _ModuleInterface,
_GlobalModuleInterface, _Thread, _run_module, _main, _get_main_modules
)

from .version import __version__ # noqa F401
12 changes: 2 additions & 10 deletions multiscanner/analytics/ssdeep_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
'''

import argparse
import configparser
import json
import logging
import sys
Expand All @@ -37,20 +36,13 @@
ssdeep = False


from multiscanner import CONFIG as MS_CONFIG
from multiscanner.common import utils
from multiscanner.storage import storage


class SSDeepAnalytic:

def __init__(self):
storage_conf = utils.get_config_path(MS_CONFIG, 'storage')
config_object = configparser.ConfigParser()
config_object.optionxform = str
config_object.read(storage_conf)
conf = utils.parse_config(config_object)
storage_handler = storage.StorageHandler(configfile=storage_conf)
storage_handler = storage.StorageHandler()
es_handler = storage_handler.load_required_module('ElasticSearchStorage')

if not es_handler:
Expand All @@ -59,7 +51,7 @@ def __init__(self):

# probably not ideal...
self.es = es_handler.es
self.index = conf['ElasticSearchStorage']['index']
self.index = es_handler.index
self.doc_type = '_doc'

def ssdeep_compare(self):
Expand Down
15 changes: 8 additions & 7 deletions multiscanner/common/dir_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

from multiscanner import CONFIG as MS_CONFIG
from multiscanner import multiscan, parse_reports
from multiscanner.common import utils
from multiscanner import config as msconf
from multiscanner.storage import storage

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -81,8 +80,7 @@ def start_observer(directory, work_queue, recursive=False):
def multiscanner_process(work_queue, config, batch_size, wait_seconds, delete, exit_signal):
filelist = []
time_stamp = None
storage_conf = utils.get_config_path(config, 'storage')
storage_handler = storage.StorageHandler(configfile=storage_conf)
storage_handler = storage.StorageHandler()
while not exit_signal.value:
time.sleep(1)
try:
Expand All @@ -102,7 +100,7 @@ def multiscanner_process(work_queue, config, batch_size, wait_seconds, delete, e
else:
continue

resultlist = multiscan(filelist, configfile=config)
resultlist = multiscan(filelist, config=config)
results = parse_reports(resultlist, python=True)
if delete:
for file_name in results:
Expand All @@ -118,13 +116,16 @@ def multiscanner_process(work_queue, config, batch_size, wait_seconds, delete, e

def _main():
args = _parse_args()
if args.config != msconf.CONFIG_FILEPATH:
msconf.update_ms_config_file(args.config)

work_queue = multiprocessing.Queue()
exit_signal = multiprocessing.Value('b')
exit_signal.value = False
observer = start_observer(args.Directory, work_queue, args.recursive)
ms_process = multiprocessing.Process(
target=multiscanner_process,
args=(work_queue, args.config, args.batch, args.seconds, args.delete, exit_signal))
args=(work_queue, msconf.MS_CONFIG, args.batch, args.seconds, args.delete, exit_signal))
ms_process.start()
try:
while True:
Expand All @@ -141,7 +142,7 @@ def _main():
def _parse_args():
parser = argparse.ArgumentParser(description='Monitor a directory and submit new files to MultiScanner')
parser.add_argument("-c", "--config", help="The config file to use", required=False,
default=MS_CONFIG)
default=msconf.CONFIG_FILEPATH)
parser.add_argument("-s", "--seconds", help="The number of seconds to wait for additional files",
required=False, default=120, type=int)
parser.add_argument("-b", "--batch", help="The max number of files per batch", required=False,
Expand Down
9 changes: 4 additions & 5 deletions multiscanner/common/pdf_generator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
from __future__ import (division, absolute_import, with_statement,
print_function, unicode_literals)

import json
import os

from reportlab.lib import colors, units
from reportlab.platypus import TableStyle

from multiscanner.common.pdf_generator import generic_pdf
from multiscanner import config as msconf


def create_pdf_document(DIR, report):
'''
Method to create a PDF report based of a multiscanner JSON report.
Args:
DIR: Represents the a directory containing the 'pdf_config.json' file.
DIR: Represents the a directory containing the 'pdf_config.ini' file.
report: A JSON object.
'''
with open(os.path.join(os.path.split(DIR)[0], 'pdf_config.json')) as data_file:
pdf_components = json.load(data_file)

pdf_config = os.path.join(DIR, 'pdf_config.ini')
pdf_components = msconf.read_config(pdf_config).get_section('pdf')
gen_pdf = generic_pdf.GenericPDF(pdf_components)

notice = []
Expand Down
49 changes: 5 additions & 44 deletions multiscanner/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import (absolute_import, division, unicode_literals, with_statement)

import ast
import configparser
import imp
import logging
import os
import sys

from multiscanner.config import PY3
from six import PY3

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -76,42 +73,6 @@ def convert_encoding(data, encoding='UTF-8', errors='replace'):
return data


def parse_config(config_object):
"""Take a config object and returns it as a dictionary"""
return_var = {}
for section in config_object.sections():
section_dict = dict(config_object.items(section))
for key in section_dict:
try:
section_dict[key] = ast.literal_eval(section_dict[key])
except Exception as e:
logger.debug(e)
return_var[section] = section_dict
return return_var


def get_config_path(config_file, component):
"""Gets the location of the config file for the given multiscanner component
from the multiscanner config file
Components:
storage
api
web"""
conf = configparser.ConfigParser()
conf.read(config_file)
conf = parse_config(conf)
try:
return conf['main']['%s-config' % component]
except KeyError:
logger.error(
"Couldn't find '{}-config' value in 'main' section "
"of config file. Have you run 'python multiscanner.py init'?"
.format(component)
)
sys.exit()


def dirname(path):
"""OS independent version of os.path.dirname"""
split = path.split('/')
Expand All @@ -134,7 +95,7 @@ def basename(path):
return split[-1]


def parseDir(directory, recursive=False, exclude=['__init__']):
def parse_dir(directory, recursive=False, exclude=['__init__']):
"""
Returns a list of files in a directory.
Expand All @@ -148,7 +109,7 @@ def parseDir(directory, recursive=False, exclude=['__init__']):
item = os.path.join(directory, item)
if os.path.isdir(item):
if recursive:
filelist.extend(parseDir(item, recursive))
filelist.extend(parse_dir(item, recursive))
else:
continue
else:
Expand All @@ -162,7 +123,7 @@ def parseDir(directory, recursive=False, exclude=['__init__']):
return filelist


def parseFileList(FileList, recursive=False):
def parse_file_list(FileList, recursive=False):
"""
Takes a list of files and directories and returns a list of files.
Expand All @@ -173,7 +134,7 @@ def parseFileList(FileList, recursive=False):
filelist = []
for item in FileList:
if os.path.isdir(item):
filelist.extend(parseDir(item, recursive))
filelist.extend(parse_dir(item, recursive))
elif os.path.isfile(item):
if not PY3:
filelist.append(item.decode('utf8'))
Expand Down
Loading

0 comments on commit a33f818

Please sign in to comment.