Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding "--config-dir" flag to CLI to support specifying path for config files #1224

Merged
merged 2 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
import sys

from streamalert import __version__ as version
from streamalert_cli.config import DEFAULT_CONFIG_PATH
from streamalert_cli.runner import cli_runner, StreamAlertCLICommandRepository
from streamalert_cli.utils import generate_subparser
from streamalert_cli.utils import DirectoryType, generate_subparser


def build_parser():
Expand All @@ -51,7 +52,6 @@ def build_parser():

{} [command] --help
"""

parser = ArgumentParser(
formatter_class=RawDescriptionHelpFormatter,
prog=__file__
Expand All @@ -71,6 +71,14 @@ def build_parser():
action='store_true'
)

parser.add_argument(
'-c',
'--config-dir',
default=DEFAULT_CONFIG_PATH,
help='Path to directory containing configuration files',
type=DirectoryType()
)

# Dynamically generate subparsers, and create a 'commands' block for the prog description
command_block = []
subparsers = parser.add_subparsers(dest='command', required=True)
Expand Down
8 changes: 4 additions & 4 deletions streamalert/shared/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ def load_config(conf_dir='conf/', exclude=None, include=None, validate=True):
]

if not (conf_files or include_clusters or schema_files):
available_files = ', '.join("'{}'".format(name) for name in sorted(default_files))
raise ConfigError('No config files to load. This is likely due the misuse of '
'the \'include\' or \'exclude\' keyword arguments. Available '
'files are: {}, clusters, and schemas.'.format(available_files))
raise ConfigError(
'No config files to load. The supplied directory could be incorrect or this could '
'be due the misuse of the \'include\' or \'exclude\' keyword arguments.'
)

config = defaultdict(dict)
for name in conf_files:
Expand Down
4 changes: 2 additions & 2 deletions streamalert_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
from streamalert_cli.apps.helpers import save_app_auth_info

LOGGER = get_logger(__name__)
DEFAULT_CONFIG_PATH = 'conf'


class CLIConfig:
"""A class to load, modify, and display the StreamAlertCLI Config"""
DEFAULT_CONFIG_PATH = 'conf/'

def __init__(self, config_path=DEFAULT_CONFIG_PATH):
def __init__(self, config_path):
self.config_path = config_path
self.config = config.load_config(config_path)

Expand Down
2 changes: 1 addition & 1 deletion streamalert_cli/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def cli_runner(args):
Returns:
bool: False if errors occurred, True otherwise
"""
config = CLIConfig()
config = CLIConfig(args.config_dir)

set_logger_levels(args.debug)

Expand Down
4 changes: 1 addition & 3 deletions streamalert_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ def __call__(self, value):
if os.path.isdir(value):
return value

raise ArgumentTypeError(
'\'%(filename)s\' is not a directory' % {'filename': value}
)
raise ArgumentTypeError('\'{}\' is not a directory'.format(value))


def add_timeout_arg(parser):
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/streamalert_cli/test_cli_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ def setup(self):
self.fs_patcher.setUp()

self.fs_patcher.fs.create_file(
'/conf/global.json', contents=json.dumps(config_data['global']))
'./conf/global.json', contents=json.dumps(config_data['global']))
self.fs_patcher.fs.create_file(
'/conf/threat_intel.json', contents=json.dumps(config_data['threat_intel']))
'./conf/threat_intel.json', contents=json.dumps(config_data['threat_intel']))
self.fs_patcher.fs.create_file(
'/conf/normalized_types.json', contents=json.dumps(config_data['normalized_types']))
'./conf/normalized_types.json', contents=json.dumps(config_data['normalized_types']))
self.fs_patcher.fs.create_file(
'/conf/lambda.json', contents=json.dumps(config_data['lambda']))
'./conf/lambda.json', contents=json.dumps(config_data['lambda']))
self.fs_patcher.fs.create_file(
'/conf/clusters/prod.json', contents=json.dumps(config_data['clusters']['prod']))
'./conf/clusters/prod.json', contents=json.dumps(config_data['clusters']['prod']))

# Create the config instance after creating the fake filesystem so that
# CLIConfig uses our mocked config files instead of the real ones.
self.config = CLIConfig()
self.config = CLIConfig('./conf/')

def teardown(self):
"""Teardown after each method"""
Expand Down