Exemple #1
0
    def test__do_sasl_handshake_v1(self):
        host, port = self.kafka_host, self.kafka_port

        # setup connection with mocked send and send_bytes
        conn = AIOKafkaConnection(
            host=host, port=port, loop=self.loop,
            sasl_mechanism="PLAIN",
            sasl_plain_username="******",
            sasl_plain_password="******",
            security_protocol="SASL_PLAINTEXT"
        )
        conn.close = close_mock = mock.MagicMock()

        supported_mechanisms = ["PLAIN"]
        error_class = NoError
        auth_error_class = NoError

        @asyncio.coroutine
        def mock_send(request, expect_response=True):
            if request.API_KEY == SaslHandShakeRequest[0].API_KEY:
                assert request.API_VERSION == 1
                return SaslHandShakeResponse[1](
                    error_code=error_class.errno,
                    enabled_mechanisms=supported_mechanisms
                )
            else:
                assert request.API_KEY == SaslAuthenticateRequest[0].API_KEY
                return SaslAuthenticateResponse[0](
                    error_code=auth_error_class.errno,
                    error_message="",
                    sasl_auth_bytes=b""
                )

        conn.send = mock.Mock(side_effect=mock_send)
        conn._version_info = VersionInfo({
            SaslHandShakeRequest[0].API_KEY: [0, 1]
        })

        yield from conn._do_sasl_handshake()

        supported_mechanisms = ["GSSAPI"]
        with self.assertRaises(UnsupportedSaslMechanismError):
            yield from conn._do_sasl_handshake()
        self.assertTrue(close_mock.call_count)
        supported_mechanisms = ["PLAIN"]

        auth_error_class = IllegalSaslStateError
        close_mock.reset()

        with self.assertRaises(IllegalSaslStateError):
            yield from conn._do_sasl_handshake()
        self.assertTrue(close_mock.call_count)
        auth_error_class = NoError

        error_class = UnknownError
        close_mock.reset()

        with self.assertRaises(UnknownError):
            yield from conn._do_sasl_handshake()
        self.assertTrue(close_mock.call_count)
Exemple #2
0
    def test__do_sasl_handshake_v1(self):
        host, port = self.kafka_host, self.kafka_port

        # setup connection with mocked send and send_bytes
        conn = AIOKafkaConnection(host=host,
                                  port=port,
                                  loop=self.loop,
                                  sasl_mechanism="PLAIN",
                                  sasl_plain_username="******",
                                  sasl_plain_password="******",
                                  security_protocol="SASL_PLAINTEXT")
        conn.close = close_mock = mock.MagicMock()

        supported_mechanisms = ["PLAIN"]
        error_class = NoError
        auth_error_class = NoError

        @asyncio.coroutine
        def mock_send(request, expect_response=True):
            if request.API_KEY == SaslHandShakeRequest[0].API_KEY:
                assert request.API_VERSION == 1
                return SaslHandShakeResponse[1](
                    error_code=error_class.errno,
                    enabled_mechanisms=supported_mechanisms)
            else:
                assert request.API_KEY == SaslAuthenticateRequest[0].API_KEY
                return SaslAuthenticateResponse[0](
                    error_code=auth_error_class.errno,
                    error_message="",
                    sasl_auth_bytes=b"")

        conn.send = mock.Mock(side_effect=mock_send)
        conn._version_info = VersionInfo(
            {SaslHandShakeRequest[0].API_KEY: [0, 1]})

        yield from conn._do_sasl_handshake()

        supported_mechanisms = ["GSSAPI"]
        with self.assertRaises(UnsupportedSaslMechanismError):
            yield from conn._do_sasl_handshake()
        self.assertTrue(close_mock.call_count)
        supported_mechanisms = ["PLAIN"]

        auth_error_class = IllegalSaslStateError
        close_mock.reset()

        with self.assertRaises(IllegalSaslStateError):
            yield from conn._do_sasl_handshake()
        self.assertTrue(close_mock.call_count)
        auth_error_class = NoError

        error_class = UnknownError
        close_mock.reset()

        with self.assertRaises(UnknownError):
            yield from conn._do_sasl_handshake()
        self.assertTrue(close_mock.call_count)
Exemple #3
0
    def test__do_sasl_handshake_v0(self):
        host, port = self.kafka_host, self.kafka_port

        # setup connection with mocked send and send_bytes
        conn = AIOKafkaConnection(
            host=host, port=port, loop=self.loop,
            sasl_mechanism="PLAIN",
            sasl_plain_username="******",
            sasl_plain_password="******"
        )
        conn.close = close_mock = mock.MagicMock()

        supported_mechanisms = ["PLAIN"]
        error_class = NoError

        @asyncio.coroutine
        def mock_send(request, expect_response=True):
            return SaslHandShakeResponse[0](
                error_code=error_class.errno,
                enabled_mechanisms=supported_mechanisms
            )

        @asyncio.coroutine
        def mock_sasl_send(payload, expect_response):
            return b""

        conn.send = mock.Mock(side_effect=mock_send)
        conn._send_sasl_token = mock.Mock(side_effect=mock_sasl_send)
        conn._version_info = VersionInfo({
            SaslHandShakeRequest[0].API_KEY: [0, 0]
        })

        yield from conn._do_sasl_handshake()

        supported_mechanisms = ["GSSAPI"]
        with self.assertRaises(UnsupportedSaslMechanismError):
            yield from conn._do_sasl_handshake()
        self.assertTrue(close_mock.call_count)

        error_class = UnknownError
        close_mock.reset()

        with self.assertRaises(UnknownError):
            yield from conn._do_sasl_handshake()
        self.assertTrue(close_mock.call_count)
Exemple #4
0
    def test__do_sasl_handshake_v0(self):
        host, port = self.kafka_host, self.kafka_port

        # setup connection with mocked send and send_bytes
        conn = AIOKafkaConnection(host=host,
                                  port=port,
                                  loop=self.loop,
                                  sasl_mechanism="PLAIN",
                                  sasl_plain_username="******",
                                  sasl_plain_password="******")
        conn.close = close_mock = mock.MagicMock()

        supported_mechanisms = ["PLAIN"]
        error_class = NoError

        @asyncio.coroutine
        def mock_send(request, expect_response=True):
            return SaslHandShakeResponse[0](
                error_code=error_class.errno,
                enabled_mechanisms=supported_mechanisms)

        @asyncio.coroutine
        def mock_sasl_send(payload, expect_response):
            return b""

        conn.send = mock.Mock(side_effect=mock_send)
        conn._send_sasl_token = mock.Mock(side_effect=mock_sasl_send)
        conn._version_info = VersionInfo(
            {SaslHandShakeRequest[0].API_KEY: [0, 0]})

        yield from conn._do_sasl_handshake()

        supported_mechanisms = ["GSSAPI"]
        with self.assertRaises(UnsupportedSaslMechanismError):
            yield from conn._do_sasl_handshake()
        self.assertTrue(close_mock.call_count)

        error_class = UnknownError
        close_mock.reset()

        with self.assertRaises(UnknownError):
            yield from conn._do_sasl_handshake()
        self.assertTrue(close_mock.call_count)