def test_load_output_config_error(log_mock): """Load outputs configuration - exception""" mock = mock_open(read_data='non-json string that will raise an exception') with patch('__builtin__.open', mock): load_outputs_config() log_mock.assert_called_with('the %s file could not be loaded into json', 'outputs.json')
def test_load_output_config(): """Load outputs configuration""" config = load_outputs_config('tests/unit/conf') loaded_config_keys = sorted(config.keys()) expected_config_keys = [u'aws-lambda', u'aws-s3', u'pagerduty', u'phantom', u'slack'] assert_list_equal(loaded_config_keys, expected_config_keys)
def configure_output(options): """Configure a new output for this service Args: options (argparser): Basically a namedtuple with the service setting """ account_config = CONFIG['global']['account'] region = account_config['region'] prefix = account_config['prefix'] kms_key_alias = account_config['kms_key_alias'] # Verify that the word alias is not in the config. # It is interpolated when the API call is made. if 'alias/' in kms_key_alias: kms_key_alias = kms_key_alias.split('/')[1] # Retrieve the proper service class to handle dispatching the alerts of this services output = get_output_dispatcher(options.service, region, prefix, config_outputs.load_outputs_config()) # If an output for this service has not been defined, the error is logged # prior to this if not output: return # get dictionary of OutputProperty items to be used for user prompting props = output.get_user_defined_properties() for name, prop in props.iteritems(): # pylint: disable=protected-access props[name] = prop._replace(value=user_input( prop.description, prop.mask_input, prop.input_restrictions)) service = output.__service__ config = config_outputs.load_config(props, service) # An empty config here means this configuration already exists, # so we can ask for user input again for a unique configuration if config is False: return configure_output(options) secrets_bucket = '{}.streamalert.secrets'.format(prefix) secrets_key = output.output_cred_name(props['descriptor'].value) # Encrypt the creds and push them to S3 # then update the local output configuration with properties if config_outputs.encrypt_and_push_creds_to_s3(region, secrets_bucket, secrets_key, props, kms_key_alias): updated_config = output.format_output_config(config, props) config_outputs.update_outputs_config(config, updated_config, service) LOGGER_CLI.info( 'Successfully saved \'%s\' output configuration for service \'%s\'', props['descriptor'].value, options.service) else: LOGGER_CLI.error( 'An error occurred while saving \'%s\' ' 'output configuration for service \'%s\'', props['descriptor'].value, options.service)
def test_load_config(method_mock): """CLI - Outputs - Load config - check for existing output""" # Patch the return value of the load_outputs_config method to return # the unit testing outputs configuration method_mock.return_value = load_outputs_config(conf_dir="tests/unit/conf") props = { 'descriptor': OutputProperty('short description', 'unit_test_lambda') } loaded = load_config(props, 'aws-lambda') assert_false(loaded)
def test_load_output_config(): """CLI - Outputs - Load outputs configuration""" config = load_outputs_config('tests/unit/conf') loaded_config_keys = sorted(config.keys()) expected_config_keys = [ 'aws-firehose', 'aws-lambda', 'aws-s3', 'aws-sns', 'aws-sqs', 'pagerduty', 'phantom', 'slack' ] assert_list_equal(loaded_config_keys, expected_config_keys)
def __init__(self, context): """AlertProcessorTester initializer Args: context [namedtuple]: Constructed aws context object. The namedtuple contains an attribute of `mocked` that indicates if all dispatch calls should be mocked out instead of actually performed. If not mocked, the tests will attempt to actually send alerts to outputs. """ self.context = context self.kms_alias = 'alias/stream_alert_secrets_test' self.secrets_bucket = 'test.streamalert.secrets' self.outputs_config = load_outputs_config()
def __init__(self, config, context): """AlertProcessorTester initializer Args: context (namedtuple): Constructed aws context object. The namedtuple contains an attribute of `mocked` that indicates if all dispatch calls should be mocked out instead of actually performed. If not mocked, the tests will attempt to actually send alerts to outputs. """ self.all_tests_passed = True self.context = context self.kms_alias = 'alias/stream_alert_secrets_test' self.secrets_bucket = 'test.streamalert.secrets' self.outputs_config = load_outputs_config() self.region = config['global']['account']['region'] self._cleanup_old_secrets() self.region = config['global']['account']['region'] helpers.setup_mock_firehose_delivery_streams(config)
def test_load_output_config_error(): """CLI - Outputs - Load outputs configuration - exception""" mock = mock_open(read_data='non-json string that will raise an exception') with patch('__builtin__.open', mock): load_outputs_config()