def test_https_logging_handler_connection_error(connection_error_adapter, dummy_log_record): handler = HTTPSHandler(host='127.0.0.1', enabled_flag=True, message_type='logstash', lvl='INFO', enabled_loggers=['awx', 'activity_stream', 'job_events', 'system_tracking']) handler.setFormatter(LogstashFormatter()) handler.session.mount('http://', connection_error_adapter) buff = cStringIO.StringIO() logging.getLogger('awx.main.utils.handlers').addHandler( logging.StreamHandler(buff) ) async_futures = handler.emit(dummy_log_record) with pytest.raises(requests.exceptions.ConnectionError): [future.result() for future in async_futures] assert 'failed to emit log to external aggregator\nTraceback' in buff.getvalue() # we should only log failures *periodically*, so causing *another* # immediate failure shouldn't report a second ConnectionError buff.truncate(0) async_futures = handler.emit(dummy_log_record) with pytest.raises(requests.exceptions.ConnectionError): [future.result() for future in async_futures] assert buff.getvalue() == ''
def test_https_logging_handler_emit_without_cred(http_adapter, dummy_log_record, message_type): handler = HTTPSHandler(host='127.0.0.1', enabled_flag=True, message_type=message_type, lvl='INFO', enabled_loggers=[ 'awx', 'activity_stream', 'job_events', 'system_tracking' ]) handler.setFormatter(LogstashFormatter()) handler.session.mount('http://', http_adapter) async_futures = handler.emit(dummy_log_record) [future.result() for future in async_futures] assert len(http_adapter.requests) == 1 request = http_adapter.requests[0] assert request.url == 'http://127.0.0.1/' assert request.method == 'POST' if message_type == 'logstash': # A username + password weren't used, so this header should be missing assert 'Authorization' not in request.headers if message_type == 'splunk': assert request.headers['Authorization'] == 'Splunk None'
def test_https_logging_handler_logstash_auth_info(): handler = HTTPSHandler(message_type='logstash', username='******', password='******') handler._add_auth_information() assert isinstance(handler.session.auth, requests.auth.HTTPBasicAuth) assert handler.session.auth.username == 'bob' assert handler.session.auth.password == 'ansible'
def test_https_logging_handler_emit_splunk_with_creds(http_adapter, dummy_log_record): handler = HTTPSHandler(host='127.0.0.1', enabled_flag=True, password='******', message_type='splunk', lvl='INFO', enabled_loggers=['awx', 'activity_stream', 'job_events', 'system_tracking']) handler.setFormatter(LogstashFormatter()) handler.session.mount('http://', http_adapter) async_futures = handler.emit(dummy_log_record) [future.result() for future in async_futures] assert len(http_adapter.requests) == 1 request = http_adapter.requests[0] assert request.headers['Authorization'] == 'Splunk pass'
def test_https_logging_handler_emit_splunk_with_creds(http_adapter, dummy_log_record): handler = HTTPSHandler(host='127.0.0.1', password='******', message_type='splunk') handler.setFormatter(LogstashFormatter()) handler.session.mount('http://', http_adapter) async_futures = handler.emit(dummy_log_record) [future.result() for future in async_futures] assert len(http_adapter.requests) == 1 request = http_adapter.requests[0] assert request.headers['Authorization'] == 'Splunk pass'
def test_https_logging_handler_emit_logstash_with_creds( http_adapter, dummy_log_record): handler = HTTPSHandler(host='127.0.0.1', username='******', password='******', message_type='logstash') handler.setFormatter(LogstashFormatter()) handler.session.mount('http://', http_adapter) async_futures = handler.emit(dummy_log_record) [future.result() for future in async_futures] assert len(http_adapter.requests) == 1 request = http_adapter.requests[0] assert request.headers['Authorization'] == 'Basic %s' % base64.b64encode( "user:pass")
def post(self, request, *args, **kwargs): defaults = dict() for key in settings_registry.get_registered_settings( category_slug='logging'): try: defaults[key] = settings_registry.get_setting_field( key).get_default() except serializers.SkipField: defaults[key] = None obj = type('Settings', (object, ), defaults)() serializer = self.get_serializer(obj, data=request.data) serializer.is_valid(raise_exception=True) # Special validation specific to logging test. errors = {} for key in ['LOG_AGGREGATOR_TYPE', 'LOG_AGGREGATOR_HOST']: if not request.data.get(key, ''): errors[key] = 'This field is required.' if errors: raise ValidationError(errors) if request.data.get('LOG_AGGREGATOR_PASSWORD', '').startswith('$encrypted$'): serializer.validated_data['LOG_AGGREGATOR_PASSWORD'] = getattr( settings, 'LOG_AGGREGATOR_PASSWORD', '') try: class MockSettings: pass mock_settings = MockSettings() for k, v in serializer.validated_data.items(): setattr(mock_settings, k, v) mock_settings.LOG_AGGREGATOR_LEVEL = 'DEBUG' if mock_settings.LOG_AGGREGATOR_PROTOCOL.upper() == 'UDP': UDPHandler.perform_test(mock_settings) return Response(status=status.HTTP_201_CREATED) else: BaseHTTPSHandler.perform_test(mock_settings) except LoggingConnectivityException as e: return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(status=status.HTTP_200_OK)
def test_https_logging_handler_emit_without_cred(https_adapter, dummy_log_record, message_type): handler = HTTPSHandler(host='127.0.0.1', message_type=message_type) handler.setFormatter(LogstashFormatter()) handler.session.mount('https://', https_adapter) async_futures = handler.emit(dummy_log_record) [future.result() for future in async_futures] assert len(https_adapter.requests) == 1 request = https_adapter.requests[0] assert request.url == 'https://127.0.0.1/' assert request.method == 'POST' if message_type == 'logstash': # A username + password weren't used, so this header should be missing assert 'Authorization' not in request.headers if message_type == 'splunk': assert request.headers['Authorization'] == 'Splunk None'
def test_https_logging_handler_has_default_http_timeout(): handler = HTTPSHandler.from_django_settings(settings) assert handler.tcp_timeout == 5
def test_https_logging_handler_splunk_auth_info(): handler = HTTPSHandler(message_type='splunk', password='******') handler._add_auth_information() assert handler.session.headers['Authorization'] == 'Splunk ansible' assert handler.session.headers['Content-Type'] == 'application/json'