Exemplo n.º 1
0
 def test_connect_when_tls_and_told_to_ignore_certs_sets_expected_context_properties(
         self, socket_mocks, ignore):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.TLS_TCP, ignore)
     handler.connect_socket()
     assert socket_mocks.SSLMocks.mock_ssl_context.verify_mode == ssl.CERT_NONE
     assert not socket_mocks.SSLMocks.mock_ssl_context.check_hostname
Exemplo n.º 2
0
 def test_connect_when_tls_and_told_to_ignore_certs_creates_context_with_none_certs(
         self, socket_mocks, ignore):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.TLS_TCP, ignore)
     handler.connect_socket()
     socket_mocks.SSLMocks.context_creator.assert_called_once_with(
         cafile=None)
Exemplo n.º 3
0
 def test_connect_socket_when_udp_initializes_with_expected_properties(
         self, socket_mocks):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.UDP, None)
     handler.connect_socket()
     call_args = socket_mocks.socket_initializer.call_args[0]
     assert call_args[0] == socket
     assert call_args[2] == SOCK_DGRAM
     assert call_args[3] == IPPROTO_UDP
Exemplo n.º 4
0
 def test_connect_socket_when_tcp_or_tls_sets_timeout_for_connection_and_resets(
         self, socket_mocks, protocol):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT, protocol,
                                       None)
     handler.connect_socket()
     call_args = socket_mocks.mock_socket.settimeout.call_args_list
     assert len(call_args) == 2
     assert call_args[0][0][0] == 10
     assert call_args[1][0][0] is None
Exemplo n.º 5
0
 def test_connect_socket_when_tcp_initializes_with_expected_properties(
         self, socket_mocks, protocol):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT, protocol,
                                       None)
     handler.connect_socket()
     call_args = socket_mocks.socket_initializer.call_args[0]
     assert call_args[0] == socket
     assert call_args[2] == SOCK_STREAM
     assert call_args[3] == IPPROTO_TCP
     assert socket_mocks.mock_socket.connect.call_count == 1
Exemplo n.º 6
0
 def test_emit_when_tcp_calls_socket_sendall_with_expected_message(
         self, mock_file_event_log_record, protocol):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT, protocol,
                                       None)
     handler.connect_socket()
     formatter = FileEventDictToRawJSONFormatter()
     handler.setFormatter(formatter)
     handler.emit(mock_file_event_log_record)
     expected_message = (formatter.format(mock_file_event_log_record) +
                         "\n").encode("utf-8")
     handler.socket.sendall.assert_called_once_with(expected_message)
Exemplo n.º 7
0
 def test_emit_when_udp_calls_socket_sendto_with_expected_message_and_address(
         self, mock_file_event_log_record):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.UDP, None)
     handler.connect_socket()
     formatter = FileEventDictToRawJSONFormatter()
     handler.setFormatter(formatter)
     handler.emit(mock_file_event_log_record)
     expected_message = (formatter.format(mock_file_event_log_record) +
                         "\n").encode("utf-8")
     handler.socket.sendto.assert_called_once_with(expected_message,
                                                   (_TEST_HOST, _TEST_PORT))
Exemplo n.º 8
0
def get_logger_for_server(hostname, protocol, output_format, certs):
    """Gets the logger that sends logs to a server for the given format.

    Args:
        hostname: The hostname of the server. It may include the port.
        protocol: The transfer protocol for sending logs.
        output_format: CEF, JSON, or RAW_JSON. Each type results in a different logger instance.
        certs: Use for passing SSL/TLS certificates when connecting to the server.
    """
    logger = logging.getLogger(f"code42_syslog_{output_format.lower()}")
    if logger_has_handlers(logger):
        return logger

    with logger_deps_lock:
        url_parts = get_url_parts(hostname)
        hostname = url_parts[0]
        port = url_parts[1] or 514
        if not logger_has_handlers(logger):
            handler = NoPrioritySysLogHandler(hostname, port, protocol, certs)
            handler.connect_socket()
            return _init_logger(logger, handler, output_format)
    return logger
Exemplo n.º 9
0
 def test_close_globally_closes(self, mocker):
     global_close = mocker.patch(
         "code42cli.logger.handlers.logging.Handler.close")
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.UDP, None)
     handler.connect_socket()
     handler.close()
     assert global_close.call_count == 1
Exemplo n.º 10
0
 def test_init_sets_socket_to_none(self):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.UDP, None)
     assert handler.socket is None
Exemplo n.º 11
0
 def test_init_when_udp_sets_expected_sock_type(self):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.UDP, None)
     actual = handler.socktype
     assert actual == SocketKind.SOCK_DGRAM
Exemplo n.º 12
0
 def test_init_when_stream_based_sets_expected_sock_type(self, protocol):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT, protocol,
                                       None)
     actual = handler.socktype
     assert actual == SocketKind.SOCK_STREAM
Exemplo n.º 13
0
 def test_init_sets_expected_address(self):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.UDP, None)
     assert handler.address == (_TEST_HOST, _TEST_PORT)
Exemplo n.º 14
0
 def test_close_when_not_using_tls_does_not_unwrap_socket(self, protocol):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT, protocol,
                                       None)
     handler.connect_socket()
     handler.close()
     assert not handler.socket.unwrap.call_count
Exemplo n.º 15
0
 def test_close_when_using_tls_unwraps_socket(self):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.TLS_TCP, None)
     handler.connect_socket()
     handler.close()
     assert handler.socket.unwrap.call_count == 1
Exemplo n.º 16
0
 def test_connect_socket_only_connects_once(self, socket_mocks):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.UDP, None)
     handler.connect_socket()
     handler.connect_socket()
     assert socket_mocks.socket_initializer.call_count == 1
Exemplo n.º 17
0
 def test_init_when_not_tls_sets_wrap_socket_to_false(self, protocol):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT, protocol,
                                       None)
     assert not handler._wrap_socket
Exemplo n.º 18
0
 def test_connect_when_tls_calls_create_default_context(self, socket_mocks):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.TLS_TCP, "certs")
     handler.connect_socket()
     call_args = socket_mocks.SSLMocks.context_creator.call_args
     assert call_args[1]["cafile"] == "certs"
Exemplo n.º 19
0
 def test_handle_error_when_connection_reset_error_occurs_raises_expected_error(
         self, mock_file_event_log_record, connection_reset_error):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.UDP, None)
     with pytest.raises(SyslogServerNetworkConnectionError):
         handler.handleError(mock_file_event_log_record)
Exemplo n.º 20
0
 def test_init_when_using_tls_sets_wrap_socket_to_true(self):
     handler = NoPrioritySysLogHandler(_TEST_HOST, _TEST_PORT,
                                       ServerProtocol.TLS_TCP, _TEST_CERTS)
     assert handler._wrap_socket
     assert handler._certs == _TEST_CERTS