def update_config(options, config, properties, output, service): """Update the local config files Args: options (argparse.Namespace): Basically a namedtuple with the service setting config (StreamAlert.config): The configuration of StreamAlert properties (OrderedDict): Contains various OutputProperty items output (StreamAlert.OutputDispatcher): The output to update service (str): The name of the service the output belongs too """ output_config = config['outputs'] descriptor = properties['descriptor'].value if options.update and output_exists( output_config, properties, service, log_message=False): # Don't update the config if the output already existed, this will prevent duplicates LOGGER.debug( 'Output already exists, don\'t update the config for descriptor %s and service %s', descriptor, service) else: updated_config = output.format_output_config( output_config, properties) output_config[service] = updated_config config.write() LOGGER.debug( 'Successfully saved \'%s\' output configuration for service \'%s\'', descriptor, service)
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
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