def test_get_log_format(self): config = {'dls_settings': {'log_format': 'JSON'}} Config.set_config(config) log_format = Config.get_log_format() self.assertEqual(log_format, 'JSON')
def main(): """ Kicks off DuoLogSync by setting important variables, creating and running a Producer-Consumer pair for each log-type defined in a config file passed to the program. """ arg_parser = argparse.ArgumentParser(prog='duologsync', description="Path to config file") arg_parser.add_argument('ConfigPath', metavar='config-path', type=str, help='Config to start application') args = arg_parser.parse_args() # Handle shutting down the program via Ctrl-C signal.signal(signal.SIGINT, sigint_handler) # Create a config Dictionary from a YAML file located at args.ConfigPath config = Config.create_config(args.ConfigPath) Config.set_config(config) # Do extra checks for Trust Monitor support is_dtm_in_config = check_for_specific_endpoint('trustmonitor', config) log_format = Config.get_log_format() is_msp = Config.account_is_msp() if (is_dtm_in_config and log_format != 'JSON'): Program.log(f"DuoLogSync: Trust Monitor endpoint only supports JSON", logging.WARNING) return if (is_dtm_in_config and is_msp): Program.log( f"DuoLogSync: Trust Monitor endpoint only supports non-msp", logging.WARNING) return Program.setup_logging(Config.get_log_filepath()) # Dict of writers (server id: writer) to be used for consumer tasks server_to_writer = Writer.create_writers(Config.get_servers()) # List of Producer/Consumer objects as asyncio tasks to be run tasks = create_tasks(server_to_writer) # Run the Producers and Consumers asyncio.get_event_loop().run_until_complete(asyncio.gather(*tasks)) asyncio.get_event_loop().close() if Program.is_logging_set(): print(f"DuoLogSync: shutdown successfully. Check " f"{Config.get_log_filepath()} for program logs")
def create_consumer_producer_pair(endpoint, writer, admin, child_account=None): """ Create a pair of Producer-Consumer objects for each endpoint and return a list containing the asyncio tasks for running those objects. @param endpoint Log type to create producer/consumer pair for @param writer Object for writing logs to a server @param admin Object from which to get the correct API endpoints @param child_account If present, this is being used by MSP and pass appropriate account id @return list of asyncio tasks for running the Producer and Consumer objects """ # The format a log should have before being consumed and sent log_format = Config.get_log_format() log_queue = asyncio.Queue() producer = consumer = None # Create the right pair of Producer-Consumer objects based on endpoint if endpoint == Config.AUTH: if Config.account_is_msp(): producer = AuthlogProducer(admin.json_api_call, log_queue, child_account_id=child_account, url_path="/admin/v2/logs/authentication") else: producer = AuthlogProducer(admin.get_authentication_log, log_queue) consumer = AuthlogConsumer(log_format, log_queue, writer, child_account) elif endpoint == Config.TELEPHONY: if Config.account_is_msp(): producer = TelephonyProducer(admin.json_api_call, log_queue, child_account_id=child_account, url_path='/admin/v1/logs/telephony') else: producer = TelephonyProducer(admin.get_telephony_log, log_queue) consumer = TelephonyConsumer(log_format, log_queue, writer, child_account) elif endpoint == Config.ADMIN: if Config.account_is_msp(): producer = AdminactionProducer(admin.json_api_call, log_queue, child_account_id=child_account, url_path='/admin/v1/logs/administrator') else: producer = AdminactionProducer(admin.get_administrator_log, log_queue) consumer = AdminactionConsumer(log_format, log_queue, writer, child_account) else: Program.log(f"{endpoint} is not a recognized endpoint", logging.WARNING) del log_queue return [] tasks = [asyncio.ensure_future(producer.produce()), asyncio.ensure_future(consumer.consume())] return tasks
def create_consumer_producer_pair(endpoint, writer, admin): """ Create a pair of Producer-Consumer objects for each endpoint and return a list containing the asyncio tasks for running those objects. @param endpoint Log type to create producer/consumer pair for @param writer Object for writing logs to a server @param admin Object from which to get the correct API endpoints @return list of asyncio tasks for running the Producer and Consumer objects """ # The format a log should have before being consumed and sent log_format = Config.get_log_format() log_queue = asyncio.Queue() producer = consumer = None # Create the right pair of Producer-Consumer objects based on endpoint if endpoint == Config.AUTH: producer = AuthlogProducer(admin.get_authentication_log, log_queue) consumer = AuthlogConsumer(log_format, log_queue, writer) elif endpoint == Config.TELEPHONY: producer = TelephonyProducer(admin.get_telephony_log, log_queue) consumer = TelephonyConsumer(log_format, log_queue, writer) elif endpoint == Config.ADMIN: producer = AdminactionProducer(admin.get_administrator_log, log_queue) consumer = AdminactionConsumer(log_format, log_queue, writer) else: Program.log(f"{endpoint} is not a recognized endpoint", logging.WARNING) del log_queue return [] tasks = [asyncio.ensure_future(producer.produce()), asyncio.ensure_future(consumer.consume())] return tasks