Esempio n. 1
0
    def handler(cls, options, config):
        """Generate a skeleton file for use with set-from-file
        Args:
            options (argparse.Namespace): Basically a namedtuple with the service setting
            config (StreamAlert.config): The configuration of StreamAlert
        Returns:
            bool: False if errors occurred, True otherwise
        """

        skeleton = {}
        for service in options.services:
            # Retrieve the proper service class to handle dispatching the alerts of this services
            # No need to safeguard, as choices are defined on --services
            output = StreamAlertOutput.get_dispatcher(service)

            # get dictionary of OutputProperty items to be used for user prompting
            properties = output.get_user_defined_properties()
            skeleton[service] = [{
                name:
                'desc: {}, restrictions: {}'.format(prop.description,
                                                    prop.input_restrictions)
                for name, prop in properties.items()
            }]

        try:
            with open(options.file, 'w') as json_file_fp:
                json.dump(skeleton, json_file_fp, indent=2, sort_keys=True)
        except Exception as err:  # pylint: disable=broad-except
            LOGGER.error(err)
            return False

        LOGGER.info(
            'Successfully generated the Skeleton file %s for services: %s',
            options.file, options.services)
        return True
Esempio n. 2
0
    def handler(cls, options, config):
        """Configure multiple outputs for multiple services
        Args:
            options (argparse.Namespace): Basically a namedtuple with the service setting
            config (StreamAlert.config): The configuration of StreamAlert
        Returns:
            bool: False if errors occurred, True otherwise
        """
        try:
            with open(options.file, 'r') as json_file_fp:
                file_contents = json.load(json_file_fp)
        except Exception:  # pylint: disable=broad-except
            LOGGER.error("Error opening file %s", options.file)
            return False

        if not file_contents:
            LOGGER.error('File %s is empty', options.file)
            return False

        for service, configurations in file_contents.items():
            LOGGER.debug('Setting outputs for service %s', service)
            # Retrieve the proper service class to handle dispatching the alerts of this service
            output = StreamAlertOutput.get_dispatcher(service)

            for configuration in configurations:
                properties = cls.convert_configuration_to_properties(
                    configuration, output)
                if not properties:
                    # Configuration was not valid
                    return False

                if not options.update and output_exists(
                        config['outputs'], properties, service):
                    # If the output already exists and update is not set
                    # return early
                    return False

                # For each configuration for this service, save the creds and update the config
                if not cls.save_credentials(service, config, properties):
                    return False
                cls.update_config(options, config, properties, output, service)

            LOGGER.info('Saved %s configurations for service: %s',
                        len(configurations), service)

        LOGGER.info('Finished setting all configurations for services: %s',
                    file_contents.keys())
        return True
Esempio n. 3
0
    def handler(cls, options, config):
        """Configure a new output for this service
        Args:
            options (argparse.Namespace): Basically a namedtuple with the service setting
            config (StreamAlert.config): The configuration of StreamAlert
        Returns:
            bool: False if errors occurred, True otherwise
        """
        # Retrieve the proper service class to handle dispatching the alerts of this services
        output = StreamAlertOutput.get_dispatcher(options.service)

        # If an output for this service has not been defined, the error is logged
        # prior to this
        if not output:
            return False

        # get dictionary of OutputProperty items to be used for user prompting
        properties = output.get_user_defined_properties()

        for name, prop in properties.items():
            # pylint: disable=protected-access
            properties[name] = prop._replace(value=user_input(
                prop.description, prop.mask_input, prop.input_restrictions))

        service = output.__service__

        if not options.update and output_exists(config['outputs'], properties,
                                                service):
            # If the output already exists and update is not set
            # ask for user input again for a unique configuration
            return cls.handler(options, config)

        if not cls.save_credentials(service, config, properties):
            # Error message is already logged so no need to log a new one
            return False

        cls.update_config(options, config, properties, output, service)

        LOGGER.info(
            'Successfully saved \'%s\' output configuration for service \'%s\'',
            properties['descriptor'].value, service)
        return True
Esempio n. 4
0
def test_get_dispatcher_bad(log_mock):
    """StreamAlertOutput - Get Invalid Dispatcher"""
    dispatcher = StreamAlertOutput.get_dispatcher('aws-s4')
    assert_is_none(dispatcher)
    log_mock.assert_called_with('Designated output service [%s] does not exist', 'aws-s4')
Esempio n. 5
0
def test_get_dispatcher_good():
    """StreamAlertOutput - Get Valid Dispatcher"""
    dispatcher = StreamAlertOutput.get_dispatcher('aws-s3')
    assert_is_not_none(dispatcher)