Commit 1c2a110f authored by Trevor Cappallo's avatar Trevor Cappallo
Browse files

fix dangerous config file load

parent 3585cb3b
......@@ -70,6 +70,10 @@ class OperationalLogger:
logging.basicConfig(filename=log_filename, format=log_format_file, datefmt=date_format_file, level=logging.DEBUG)
formatter = logging.Formatter(fmt=log_format_console, datefmt=date_format_console)
# sub_handler is used for output from log_shell.
# It is passed parentname and parentline in 'extra' dict,
# corresponding to file and line of code calling log_shell.
formatter_sub = logging.Formatter(
#fmt='%(asctime)s [%(parentname)s:%(parentline)d] :: %(message)s',
fmt='%(asctime)s |- %(message)s',
......@@ -79,11 +83,13 @@ class OperationalLogger:
sub_handler.setLevel(logging.INFO)
sub_handler.setFormatter(formatter_sub)
logging.getLogger('_sub').addHandler(sub_handler)
# Turn off propagation so root handler is not triggered.
logging.getLogger('_sub').propagate = False
full_trigger = logging.StreamHandler(self.__debug_buffer)
full_trigger.setLevel(logging.DEBUG)
full_trigger.setFormatter(formatter)
debug_trigger = logging.StreamHandler(self.__debug_buffer)
debug_trigger.setLevel(logging.DEBUG)
debug_trigger.setFormatter(formatter)
error_trigger = logging.StreamHandler(self.__error_buffer)
error_trigger.setLevel(logging.ERROR)
error_trigger.setFormatter(formatter)
......@@ -93,7 +99,7 @@ class OperationalLogger:
logging.getLogger(logger_name).addHandler(error_trigger)
logging.getLogger(logger_name).addHandler(critical_trigger)
logging.getLogger(logger_name).addHandler(full_trigger)
logging.getLogger(logger_name).addHandler(debug_trigger)
# provide a more concise format for terminal output
if sys.stdout.isatty():
......@@ -175,20 +181,33 @@ def setup(config_file=None, logger_name=None, log_filename=None, log_root_dir=No
os.makedirs(log_directory, 02750)
except OSError:
raise RuntimeError('Unable to create log directory: {}'.format(log_directory))
if notify is None:
notify = {}
if logger_name is None:
logger_name = ''
if log_level is None:
log_level = logging.DEBUG
if console_level is None:
console_level = logging.INFO
# Load config{} with passed args, defaulting to None.
config = {
'log_filename': log_filename, 'logger_name': logger_name,
'from_address': from_address, 'notify': notify,
'log_level': log_level, 'console_level': console_level,
'log_format_file': log_format_file, 'date_format_file': date_format_file,
'log_format_console': log_format_console, 'date_format_console': date_format_console,
}
# If config file provided, replace any None values with loaded value.
if config_file:
with open(config_file, 'r') as f:
config = json.load(f)
for key, val in config.items():
if key in vars() and vars()[key] is None:
vars()[key] = val
loaded_config = json.load(f)
for key, val in loaded_config.items():
if key in config and val is None:
config[key] = val
# Set reasonable defaults for remaining Nones.
if config['log_filename'] is None:
config['log_filename'] = os.path.join(log_directory, log_filename)
if config['logger_name'] is None:
config['logger_name'] = ''
if config['notify'] is None:
config['notify'] = {}
if config['log_level'] is None:
config['log_level'] = logging.DEBUG
if config['console_level'] is None:
config['console_level'] = logging.INFO
logger = OperationalLogger(
log_filename=os.path.join(log_directory, log_filename), logger_name=logger_name,
......
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