def test_should_log_critical_message_if_log_level_is_below_info( self, mock_stdout, mock_config): unsafe_log_levels = ['NOTSET', 'DEBUG'] for level in unsafe_log_levels: def config_values(*args, **kwargs): return { "LOG_LEVEL": level, "LOG_FORMAT": log.LOG_FORMAT_STRING }[args[0]] mock_stdout.truncate(0) with self.subTest( f'Log level {level} should result in critical log message being logged out' ): mock_config.side_effect = config_values log.configure_logging() output = mock_stdout.getvalue() log_entry = LogEntry(output) self.assertEqual('CRITICAL', log_entry.level) self.assertEqual( f'The current log level ({level}) is set below INFO level,' f' it is known that libraries used ' 'by this application sometimes log out clinical patient data at DEBUG level. ' 'The log level provided MUST NOT be used in a production environment.', log_entry.message)
def test_log_threshold(self, mock_stdout): config.config['LOG_LEVEL'] = 'AUDIT' log.configure_logging() log.IntegrationAdaptorsLogger('TES').info('100', 'Test message') output = mock_stdout.getvalue() self.assertEqual("", output)
def main(): config.setup_config("MHS") secrets.setup_secret_config("MHS") log.configure_logging() if config.get_config('NO_TLS', default='False') == 'True': certificates = certs.Certs() else: certificates = certs.Certs.create_certs_files( definitions.ROOT_DIR, private_key=secrets.get_secret_config('CLIENT_KEY'), local_cert=secrets.get_secret_config('CLIENT_CERT'), ca_certs=secrets.get_secret_config('CA_CERTS')) party_key = secrets.get_secret_config('PARTY_KEY') workflows = initialise_workflows() store = dynamo_persistence_adaptor.DynamoPersistenceAdaptor( table_name=config.get_config('STATE_TABLE_NAME')) interactions_config_file = pathlib.Path( definitions.ROOT_DIR) / 'data' / "interactions" / "interactions.json" config_manager = configuration_manager.ConfigurationManager( str(interactions_config_file)) start_inbound_server(certificates.local_cert_path, certificates.ca_certs_path, certificates.private_key_path, party_key, workflows, store, config_manager)
def main(): config.setup_config('SCR') secrets.setup_secret_config("SCR") log.configure_logging() app = build_app() app.listen(80) tornado.ioloop.IOLoop.current().start()
def test_empty_values(self, mock_stdout): log.configure_logging() logger = log.IntegrationAdaptorsLogger('SYS') with self.subTest('Empty info log'): logger.info('100', 'I can still log info strings without values!') output = mock_stdout.getvalue() self.assertIn('I can still log info strings without values!', output) with self.subTest('Empty audit log'): logger.audit('100', 'I can still log audit strings without values!') output = mock_stdout.getvalue() self.assertIn('I can still log audit strings without values!', output) with self.subTest('Empty warning log'): logger.warning('100', 'I can still log warning strings without values!') output = mock_stdout.getvalue() self.assertIn('I can still log warning strings without values!', output) with self.subTest('Empty error log'): logger.error('100', 'I can still log error strings without values!') output = mock_stdout.getvalue() self.assertIn('I can still log error strings without values!', output) with self.subTest('Empty Critical log'): logger.critical( '100', 'I can still log critical strings without values!') output = mock_stdout.getvalue() self.assertIn('I can still log critical strings without values!', output)
def main(): config.setup_config("SDS") secrets.setup_secret_config("SDS") log.configure_logging('sds') sds_client = lookup.sds_client_factory.get_sds_client() start_tornado_server(sds_client)
def main(): config.setup_config("MHS") secrets.setup_secret_config("MHS") log.configure_logging('spine-directory-service') routing = initialise_routing(search_base=config.get_config("SDS_SEARCH_BASE")) start_tornado_server(routing)
def test_format_and_write(self, mock_std): log.configure_logging() log.message_id.set('10') log.correlation_id.set('5') log.inbound_message_id.set('8') log.IntegrationAdaptorsLogger('SYS')._format_and_write( message='{yes} {no} {maybe}', values={ 'yes': 'one', 'no': 'two', 'maybe': 'three' }, level=logging.INFO, process_key_num='100') output = mock_std.getvalue() # Check that each value has spaces self.assertIn(' CorrelationId=5 ', output) self.assertIn(' LogLevel=INFO ', output) self.assertIn(' ProcessKey=SYS100', output) self.assertIn(' RequestId=10 ', output) self.assertIn(' maybe=three ', output) self.assertIn(' yes=one ', output) self.assertIn(f' pid={os.getpid()}', output) self.assertIn('InboundMessageId=8', output)
def test_correct_time_format(self, mock_stdout): log.configure_logging() logger = log.IntegrationAdaptorsLogger('SYS') logger.info('100', 'I can still log info strings without values!') output = mock_stdout.getvalue() time_value = output.split('[')[1].split(']')[0] time.strptime(time_value, '%Y-%m-%dT%H:%M:%S.%fZ')
def test_name_can_be_empty(self, mock_stdout): mock_stdout.truncate(0) log.configure_logging() log.IntegrationAdaptorsLogger('SYS').info('%s %s', 'yes', 'no') log_entry = LogEntry(mock_stdout.getvalue()) self.assertEqual('SYS', log_entry.name)
def setup_logger(self, mock_environ): mock_environ["PREFIX_LOG_LEVEL"] = "INFO" config.setup_config("PREFIX") def remove_logging_handler(): logging.getLogger().handlers = [] self.addCleanup(remove_logging_handler) integration_adaptors_logger.configure_logging()
def test_log_level_threshold(self, mock_stdout): mock_stdout.truncate(0) config.config['LOG_LEVEL'] = 'CRITICAL' log.configure_logging() log.IntegrationAdaptorsLogger('TES').info('Test message') config.config['LOG_LEVEL'] = 'INFO' output = mock_stdout.getvalue() self.assertEqual("", output)
def test_should_not_log_critical_message_if_log_level_is_above_debug( self, mock_stdout, mock_config): safe_log_levels = ['INFO', 'AUDIT', 'WARNING', 'ERROR', 'CRITICAL'] for level in safe_log_levels: with self.subTest( f'Log level {level} should not result in critical log message being logged out' ): mock_config.return_value = level log.configure_logging() output = mock_stdout.getvalue() self.assertEqual('', output)
def test_custom_audit_level(self, mock_stdout): log.configure_logging() log.correlation_id.set('2') log.IntegrationAdaptorsLogger('TES') \ .audit('100', '{There Will Be No Spaces Today}', {'There Will Be No Spaces Today': 'wow qwe'}) output = mock_stdout.getvalue() self.assertIn('CorrelationId=2', output) self.assertIn('ThereWillBeNoSpacesToday="wow qwe"', output) self.assertIn('LogLevel=AUDIT', output) self.assertIn('ProcessKey=TES100', output)
def test_interaction_id_context_var_should_be_logged_when_set( self, mock_stdout): # Arrange log.configure_logging() log.interaction_id.set('GP_101') logger = log.IntegrationAdaptorsLogger('TES') # Act logger.audit('100', 'This log message should have interaction id ') output = mock_stdout.getvalue() # Assert self.assertIn('InteractionId=GP_101', output)
def test_should_not_log_critical_message_if_log_level_is_above_debug(self, mock_stdout, mock_config): safe_log_levels = ['INFO', 'WARNING', 'ERROR', 'CRITICAL'] for level in safe_log_levels: def config_values(*args, **kwargs): return { "LOG_LEVEL": level, "LOG_FORMAT": "" }[args[0]] with self.subTest(f'Log level {level} should not result in critical log message being logged out'): mock_config.side_effect = config_values log.configure_logging() output = mock_stdout.getvalue() self.assertEqual('', output)
def test_custom_audit_level(self, mock_stdout): mock_stdout.truncate(0) log.configure_logging() mdc.correlation_id.set('2') log.IntegrationAdaptorsLogger('TES').audit( 'Some audit message with %s %s', 'additional', 'parameters') output = mock_stdout.getvalue() log_entry = LogEntry(output) self.assertEqual('2', log_entry.correlation_id) self.assertEqual('AUDIT', log_entry.level) self.assertIn('Some audit message with additional parameters', log_entry.message)
def test_message_format_and_log_entry_parts(self, mock_stdout): mock_stdout.truncate(0) log.configure_logging('TEST') mdc.correlation_id.set('15') log.IntegrationAdaptorsLogger('SYS').info('%s %s', 'yes', 'no') log_entry = LogEntry(mock_stdout.getvalue()) self.assertEqual('15', log_entry.correlation_id) self.assertEqual('TEST.SYS', log_entry.name) self.assertEqual('INFO', log_entry.level) self.assertEqual('yes no', log_entry.message) time.strptime(log_entry.time, '%Y-%m-%dT%H:%M:%S.%f')
def test_should_log_critical_message_if_log_level_is_below_info( self, mock_stdout, mock_config): unsafe_log_levels = ['NOTSET', 'DEBUG'] for level in unsafe_log_levels: with self.subTest( f'Log level {level} should result in critical log message being logged out' ): mock_config.return_value = level log.configure_logging() output = mock_stdout.getvalue() self.assertIn( f'The current log level (logLevel={level}) is set below INFO level,' f' it is known that libraries used ' 'by this application sometimes log out clinical patient data at DEBUG level. ' 'The log level provided MUST NOT be used in a production environment.', output)
def test_get_config_no_config_variable_found(self, mock_stdout, mock_environ): mock_environ["PREFIX_LOG_LEVEL"] = "INFO" config.setup_config("PREFIX") def remove_logging_handler(): logging.getLogger().handlers = [] self.addCleanup(remove_logging_handler) integration_adaptors_logger.configure_logging() with self.assertRaises(KeyError): config.get_config("BLAH") output = mock_stdout.getvalue() self.assertIn( 'Failed to get config ConfigName="BLAH" ProcessKey=CONFIG003', output) self.assertIn("LogLevel=ERROR", output)
def test_format_and_write_empty_vals(self, mock_std): log.configure_logging() log.IntegrationAdaptorsLogger('SYS')._format_and_write( message='{yes} {no} {maybe}', values={ 'yes': 'one', 'no': 'two', 'maybe': 'three' }, level=logging.INFO, process_key_num='100') output = mock_std.getvalue() self.assertIn(' LogLevel=INFO ', output) self.assertIn(' ProcessKey=SYS100', output) self.assertIn(' maybe=three ', output) self.assertIn(f' pid={os.getpid()}', output) self.assertIn(' yes=one ', output) self.assertNotIn('CorrelationId=', output) self.assertNotIn('RequestId=', output)
def main(): config.setup_config("MHS") secrets.setup_secret_config("MHS") log.configure_logging() data_dir = pathlib.Path(definitions.ROOT_DIR) / "data" configure_http_client() routing = initialise_routing() certificates = certs.Certs.create_certs_files( data_dir / '..', private_key=secrets.get_secret_config('CLIENT_KEY'), local_cert=secrets.get_secret_config('CLIENT_CERT'), ca_certs=secrets.get_secret_config('CA_CERTS')) max_retries = int( config.get_config('OUTBOUND_TRANSMISSION_MAX_RETRIES', default="3")) retry_delay = int( config.get_config('OUTBOUND_TRANSMISSION_RETRY_DELAY', default="100")) http_proxy_host = config.get_config('OUTBOUND_HTTP_PROXY', default=None) http_proxy_port = None if http_proxy_host is not None: http_proxy_port = int( config.get_config('OUTBOUND_HTTP_PROXY_PORT', default="3128")) transmission = outbound_transmission.OutboundTransmission( certificates.local_cert_path, certificates.private_key_path, certificates.ca_certs_path, max_retries, retry_delay, http_proxy_host, http_proxy_port) party_key = secrets.get_secret_config('PARTY_KEY') work_description_store = dynamo_persistence_adaptor.DynamoPersistenceAdaptor( table_name=config.get_config('STATE_TABLE_NAME')) sync_async_store = dynamo_persistence_adaptor.DynamoPersistenceAdaptor( table_name=config.get_config('SYNC_ASYNC_STATE_TABLE_NAME')) store_retries = int( config.get_config('STATE_STORE_MAX_RETRIES', default='3')) max_request_size = int(config.get_config('SPINE_REQUEST_MAX_SIZE')) workflows = initialise_workflows(transmission, party_key, work_description_store, sync_async_store, max_request_size, store_retries, routing) start_tornado_server(data_dir, workflows)
def main(args): config.setup_config('MHS') log.configure_logging('spine-directory-service') _read_real_server_data(args.path, args.nhs_id_code)