Skip to content

Commit

Permalink
Merge pull request #1209 from airbnb/dw--csq
Browse files Browse the repository at this point in the history
Make scheduled_queries/ directory configurable from global.json
  • Loading branch information
Ryxias committed Mar 27, 2020
2 parents 266c5a5 + 8f97786 commit cdeff0c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
3 changes: 3 additions & 0 deletions conf/global.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
],
"rule_locations": [
"rules"
],
"scheduled_query_locations": [
"scheduled_queries"
]
},
"infrastructure": {
Expand Down
16 changes: 10 additions & 6 deletions docs/source/config-global.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,23 @@ Configuration
],
"rule_locations": [
"rules"
],
"scheduled_query_locations": [
"scheduled_queries"
]
}
}
Options
-------
====================== ============ ================= ===============
**Key** **Required** **Default** **Description**
---------------------- ------------ ----------------- ---------------
``matcher_locations`` Yes ``["matchers"]`` List of local paths where ``matchers`` are defined
``rule_locations`` Yes ``["rules"]`` List of local paths where ``rules`` are defined
====================== ============ ================= ===============
============================= ============= ========================= ===============
**Key** **Required** **Default** **Description**
----------------------------- ------------- ------------------------- ---------------
``matcher_locations`` Yes ``["matchers"]`` List of local paths where ``matchers`` are defined
``rule_locations`` Yes ``["rules"]`` List of local paths where ``rules`` are defined
``scheduled_query_locations`` Yes ``["scheduled_queries"]`` List of local paths where ``scheduled_queries`` are defined
============================= ============= ========================= ===============


**************
Expand Down
17 changes: 15 additions & 2 deletions streamalert/scheduled_queries/config/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from streamalert.scheduled_queries.state.state_manager import StateManager, StepFunctionStateManager
from streamalert.scheduled_queries.streamalert.kinesis import KinesisClient
from streamalert.scheduled_queries.support.clock import Clock
from streamalert.shared.config import load_config


# FIXME (Ryxias)
Expand Down Expand Up @@ -86,6 +87,11 @@ def configure_container(container):
container.register(ServiceDefinition('clock', _make_clock))
container.register(ServiceDefinition('boto3_athena_client', _make_boto3_athena_client))
container.register(ServiceDefinition('boto3_kinesis_client', _make_boto3_kinesis_client))
container.register(ServiceDefinition('config', _load_config))


def _load_config(_):
return load_config()


def _make_command_processor(container):
Expand Down Expand Up @@ -135,9 +141,16 @@ def _make_param_generator(container):
return QueryParameterGenerator(container.get('logger'), container.get('clock'))


def _make_query_pack_repo(_):
def _make_query_pack_repo(container):
repo = QueryPackRepository
repo.load_packs()

config = container.get('config')
query_directories = [
item
for item in config['global']['general'].get('scheduled_query_locations', [])
]

repo.load_packs(query_directories)
return repo


Expand Down
6 changes: 2 additions & 4 deletions streamalert/scheduled_queries/query_packs/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
"""
from streamalert.shared.importer import import_folders

PACKS_DIRECTORY = 'scheduled_queries/'


class QueryPackConfiguration:

Expand Down Expand Up @@ -112,5 +110,5 @@ def register(cls, config):
cls.QUERY_PACKS[name] = config

@classmethod
def load_packs(cls):
import_folders(PACKS_DIRECTORY)
def load_packs(cls, directories):
import_folders(*directories)
3 changes: 2 additions & 1 deletion tests/unit/conf/global.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
},
"general": {
"matcher_locations": [],
"rule_locations": []
"rule_locations": [],
"scheduled_query_locations": []
},
"infrastructure": {
"alerts_table": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ class TestQueryPackRepository:
@staticmethod
def test_load_and_get_packs():
"""StreamQuery - QueryPackRepository - get_packs"""
QueryPackRepository.load_packs()
QueryPackRepository.load_packs(['scheduled_queries/'])

assert_true(len(QueryPackRepository.get_packs()) >= 1)

0 comments on commit cdeff0c

Please sign in to comment.