cc: allow logger config to be specified at runtime

This commit is contained in:
Mike Salvatore 2021-02-10 12:39:11 -05:00
parent 4cb28db3bc
commit e8bb2e6be2
4 changed files with 33 additions and 11 deletions

View File

@ -2,25 +2,43 @@ from gevent import monkey as gevent_monkey
gevent_monkey.patch_all()
import json # noqa: E402
from monkey_island.cc.main import main # noqa: E402
from monkey_island.cc.environment.environment_config import DEFAULT_SERVER_CONFIG_PATH # noqa: E402
from monkey_island.cc.consts import DEFAULT_SERVER_CONFIG_PATH, DEFAULT_LOGGING_CONFIG_PATH # noqa: E402
from monkey_island.cc.island_logger import json_setup_logging # noqa: E402
def parse_cli_args():
import argparse
parser = argparse.ArgumentParser(description="Infection Monkey Island CnC Server. See https://infectionmonkey.com",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-s", "--setup-only", action="store_true",
help="Pass this flag to cause the Island to setup and exit without actually starting. "
"This is useful for preparing Island to boot faster later-on, so for "
"compiling/packaging Islands.")
parser.add_argument("-c", "--config", action="store",
parser.add_argument("--server-config", action="store",
help="The path to the server configuration file.",
default=DEFAULT_SERVER_CONFIG_PATH)
parser.add_argument("--logging-config", action="store",
help="The path to the logging configuration file.",
default=DEFAULT_LOGGING_CONFIG_PATH)
args = parser.parse_args()
return (args.setup_only, args.config)
return (args.setup_only, args.server_config, args.logging_config)
if "__main__" == __name__:
(is_setup_only, config) = parse_cli_args()
main(is_setup_only, config)
(is_setup_only, server_config, logging_config) = parse_cli_args()
# This is here in order to catch EVERYTHING, some functions are being called on
# imports, so the log init needs to be first.
try:
json_setup_logging(logging_config)
except(json.JSONDecodeError) as ex:
print(f"Error loading logging config: {ex}")
exit(1)
from monkey_island.cc.main import main
main(is_setup_only, server_config)

View File

@ -13,11 +13,7 @@ if str(MONKEY_ISLAND_DIR_BASE_PATH) not in sys.path:
sys.path.insert(0, MONKEY_ISLAND_DIR_BASE_PATH)
from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH # noqa: E402
from monkey_island.cc.server_utils.island_logger import json_setup_logging # noqa: E402
# This is here in order to catch EVERYTHING, some functions are being called on imports the log init needs to be on top.
json_setup_logging(default_path=Path(MONKEY_ISLAND_ABS_PATH, 'cc', 'island_logger_default_config.json'),
default_level=logging.DEBUG)
logger = logging.getLogger(__name__)
import monkey_island.cc.environment.environment_singleton as env_singleton # noqa: E402

View File

@ -12,3 +12,7 @@ DEFAULT_SERVER_CONFIG_PATH = os.path.join(
DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join(
MONKEY_ISLAND_ABS_PATH, "cc", "server_config.json.develop"
)
DEFAULT_LOGGING_CONFIG_PATH = os.path.join(
MONKEY_ISLAND_ABS_PATH, "cc", "island_logger_default_config.json"
)

View File

@ -2,11 +2,15 @@ import json
import logging.config
import os
from monkey_island.cc.consts import DEFAULT_LOGGING_CONFIG_PATH
__author__ = "Maor.Rayzin"
def json_setup_logging(
default_path="logging.json", default_level=logging.INFO, env_key="LOG_CFG"
default_path=DEFAULT_LOGGING_CONFIG_PATH,
default_level=logging.INFO,
env_key="LOG_CFG",
):
"""
Setup the logging configuration
@ -15,7 +19,7 @@ def json_setup_logging(
:param env_key: SYS ENV key to use for external configuration file path
:return:
"""
path = default_path
path = os.path.expanduser(default_path)
value = os.getenv(env_key, None)
if value: