def _authenticate(self, connection):
     client = self._client
     cluster_name = self._config.cluster_name
     client_name = client.name
     if self._config.token_provider:
         token = self._config.token_provider.token(
             connection.connected_address)
         request = client_authentication_custom_codec.encode_request(
             cluster_name,
             token,
             self.client_uuid,
             CLIENT_TYPE,
             SERIALIZATION_VERSION,
             __version__,
             client_name,
             self._labels,
         )
     else:
         request = client_authentication_codec.encode_request(
             cluster_name,
             self._config.creds_username,
             self._config.creds_password,
             self.client_uuid,
             CLIENT_TYPE,
             SERIALIZATION_VERSION,
             __version__,
             client_name,
             self._labels,
         )
     invocation = Invocation(request,
                             connection=connection,
                             urgent=True,
                             response_handler=lambda m: m)
     self._invocation_service.invoke(invocation)
     return invocation.future
    def _cluster_authenticator(self, connection):
        uuid = self._client.cluster.uuid
        owner_uuid = self._client.cluster.owner_uuid

        request = client_authentication_codec.encode_request(
            username=self._client.config.group_config.name,
            password=self._client.config.group_config.password,
            uuid=uuid,
            owner_uuid=owner_uuid,
            is_owner_connection=False,
            client_type=CLIENT_TYPE,
            serialization_version=SERIALIZATION_VERSION,
            client_hazelcast_version=CLIENT_VERSION)

        def callback(f):
            parameters = client_authentication_codec.decode_response(
                f.result())
            if parameters["status"] != 0:
                raise AuthenticationError("Authentication failed.")
            connection.endpoint = parameters["address"]
            self.owner_uuid = parameters["owner_uuid"]
            self.uuid = parameters["uuid"]
            connection.server_version_str = parameters.get(
                "server_hazelcast_version", "")
            connection.server_version = calculate_version(
                connection.server_version_str)
            return connection

        return self._client.invoker.invoke_on_connection(
            request, connection).continue_with(callback)
 def test_unfragmented_message(self):
     request = client_authentication_codec.encode_request("dev", "user", "pass", uuid.uuid4(),
                                                          "PYH", 1, "4.0", "python", [])
     self.reader.read(request.buf)
     message = self.reader._read_message()
     self.builder.on_message(message)
     self.assertEqual(1, self.counter.value)
Exemplo n.º 4
0
    def test_message_accumulate(self):
        message = client_authentication_codec.encode_request(
            "user", "pass", "uuid", "owner-uuid", True, "PYH", 1, "3.10")
        message.set_correlation_id(1)

        def message_callback(merged_message):
            self.assertTrue(merged_message.is_flag_set(BEGIN_END_FLAG))
            self.assertEqual(merged_message.get_frame_length(),
                             message.get_frame_length())
            self.assertEqual(merged_message.get_correlation_id(),
                             message.get_correlation_id())

        builder = ClientMessageBuilder(message_callback=message_callback)

        header = message.buffer[0:message.get_data_offset()]
        payload = message.buffer[message.get_data_offset():]

        indx_1 = len(payload) // 3
        indx_2 = 2 * len(payload) // 3

        p1 = payload[0:indx_1]
        p2 = payload[indx_1:indx_2]
        p3 = payload[indx_2:]

        cm1 = ClientMessage(buff=header + p1)
        cm2 = ClientMessage(buff=header + p2)
        cm3 = ClientMessage(buff=header + p3)
        cm1.add_flag(BEGIN_FLAG)
        cm3.add_flag(END_FLAG)

        builder.on_message(cm1)
        builder.on_message(cm2)
        builder.on_message(cm3)
    def test_message_accumulate(self):
        message = client_authentication_codec.encode_request("user", "pass", "uuid", "owner-uuid", True, "PYH", 1)
        message.set_correlation_id(1)

        def message_callback(merged_message):
            self.assertTrue(merged_message.is_flag_set(BEGIN_END_FLAG))
            self.assertEqual(merged_message.get_frame_length(), message.get_frame_length())
            self.assertEqual(merged_message.get_correlation_id(), message.get_correlation_id())

        builder = ClientMessageBuilder(message_callback=message_callback)

        header = message.buffer[0:message.get_data_offset()]
        payload = message.buffer[message.get_data_offset():]

        indx_1 = len(payload) // 3
        indx_2 = 2 * len(payload) // 3

        p1 = payload[0:indx_1]
        p2 = payload[indx_1: indx_2]
        p3 = payload[indx_2:]

        cm1 = ClientMessage(buff=header + p1)
        cm2 = ClientMessage(buff=header + p2)
        cm3 = ClientMessage(buff=header + p3)
        cm1.add_flag(BEGIN_FLAG)
        cm3.add_flag(END_FLAG)

        builder.on_message(cm1)
        builder.on_message(cm2)
        builder.on_message(cm3)
Exemplo n.º 6
0
 def authenticate_manager(conn):
     request = client_authentication_codec.encode_request(
         username=self._config.username, password=self._config.password,
         uuid=None, owner_uuid=None, is_owner_connection=True, client_type=CLIENT_TYPE,
         serialization_version=SERIALIZATION_VERSION)
     response = self._client.invoker.invoke_on_connection(request, conn).result()
     parameters = client_authentication_codec.decode_response(response)
     if parameters["status"] != 0:
         raise RuntimeError("Authentication failed")
     conn.endpoint = parameters["address"]
     self.owner_uuid = parameters["owner_uuid"]
     self.uuid = parameters["uuid"]
Exemplo n.º 7
0
    def _authenticate(self, connection):
        client = self._client
        cluster_name = client.config.cluster_name
        client_name = client.name
        request = client_authentication_codec.encode_request(
            cluster_name, None, None, self.client_uuid, CLIENT_TYPE,
            SERIALIZATION_VERSION, __version__, client_name, self._labels)

        invocation = Invocation(request,
                                connection=connection,
                                urgent=True,
                                response_handler=lambda m: m)
        self._invocation_service.invoke(invocation)
        return invocation.future
    def _authenticate_manager(self, connection):
        request = client_authentication_codec.encode_request(
            username=self._config.group_config.name, password=self._config.group_config.password,
            uuid=self.uuid, owner_uuid=self.owner_uuid, is_owner_connection=True, client_type=CLIENT_TYPE,
            serialization_version=SERIALIZATION_VERSION)

        def callback(f):
            parameters = client_authentication_codec.decode_response(f.result())
            if parameters["status"] != 0:  # TODO: handle other statuses
                raise AuthenticationError("Authentication failed.")
            connection.endpoint = parameters["address"]
            connection.is_owner = True
            self.owner_uuid = parameters["owner_uuid"]
            self.uuid = parameters["uuid"]
            return connection

        return self._client.invoker.invoke_on_connection(request, connection).continue_with(callback)
Exemplo n.º 9
0
    def _authenticate_manager(self, connection):
        request = client_authentication_codec.encode_request(
            username=self._config.group_config.name, password=self._config.group_config.password,
            uuid=None, owner_uuid=None, is_owner_connection=True, client_type=CLIENT_TYPE,
            serialization_version=SERIALIZATION_VERSION)

        def callback(f):
            parameters = client_authentication_codec.decode_response(f.result())
            if parameters["status"] != 0:  # TODO: handle other statuses
                raise AuthenticationError("Authentication failed.")
            connection.endpoint = parameters["address"]
            connection.is_owner = True
            self.owner_uuid = parameters["owner_uuid"]
            self.uuid = parameters["uuid"]
            return connection

        return self._client.invoker.invoke_on_connection(request, connection).continue_with(callback)
    def _cluster_authenticator(self, conn):
        uuid = self._client.cluster.uuid
        owner_uuid = self._client.cluster.owner_uuid

        request = client_authentication_codec.encode_request(
            username=self._client.config.username,
            password=self._client.config.password,
            uuid=uuid,
            owner_uuid=owner_uuid,
            is_owner_connection=False,
            client_type="PHY",
            serialization_version=1)

        response = self._client.invoker.invoke_on_connection(request, conn).result()
        parameters = client_authentication_codec.decode_response(response)
        if parameters["status"] != 0:
            raise RuntimeError("Authentication failed")
        conn.endpoint = parameters["address"]
        self.owner_uuid = parameters["owner_uuid"]
        self.uuid = parameters["uuid"]
        pass
Exemplo n.º 11
0
    def _cluster_authenticator(self, connection):
        uuid = self._client.cluster.uuid
        owner_uuid = self._client.cluster.owner_uuid

        request = client_authentication_codec.encode_request(
            username=self._client.config.group_config.name,
            password=self._client.config.group_config.password,
            uuid=uuid,
            owner_uuid=owner_uuid,
            is_owner_connection=False,
            client_type=CLIENT_TYPE,
            serialization_version=1)

        def callback(f):
            parameters = client_authentication_codec.decode_response(f.result())
            if parameters["status"] != 0:
                raise AuthenticationError("Authentication failed.")
            connection.endpoint = parameters["address"]
            self.owner_uuid = parameters["owner_uuid"]
            self.uuid = parameters["uuid"]
            return connection

        return self._client.invoker.invoke_on_connection(request, connection).continue_with(callback)
Exemplo n.º 12
0
    def test_encode(self):
        msg = client_authentication_codec.encode_request(
            "dev",
            "username",
            "password",
            uuid.UUID(hex="1862c7d2-f89c-4151-981d-07a6287089d3"),
            "PYH",
            1,
            "5.0",
            "hz.client_0",
            ["label"],
        )

        # fmt: off
        expected = [
            # initial frame
            40,
            0,
            0,
            0,  # length
            0,
            192,  # flags
            0,
            1,
            0,
            0,  # message type
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,  # correlation id
            255,
            255,
            255,
            255,  # partition id
            0,
            81,
            65,
            156,
            248,
            210,
            199,
            98,
            24,
            211,
            137,
            112,
            40,
            166,
            7,
            29,
            152,  # uuid
            1,  # serialization version

            # cluster name frame
            9,
            0,
            0,
            0,  # length
            0,
            0,  # flags
            100,
            101,
            118,  # cluster name

            # username frame
            14,
            0,
            0,
            0,  # length
            0,
            0,  # length
            117,
            115,
            101,
            114,
            110,
            97,
            109,
            101,  # username

            # password frame
            14,
            0,
            0,
            0,  # length
            0,
            0,  # flags
            112,
            97,
            115,
            115,
            119,
            111,
            114,
            100,  # password

            # client type frame
            9,
            0,
            0,
            0,  # length
            0,
            0,  # flags
            80,
            89,
            72,  # client type

            # client version frame
            9,
            0,
            0,
            0,  # length
            0,
            0,  # flags
            53,
            46,
            48,  # version

            # client name frame
            17,
            0,
            0,
            0,  # length
            0,
            0,  # flags
            104,
            122,
            46,
            99,
            108,
            105,
            101,
            110,
            116,
            95,
            48,  # client name

            # labels begin frame
            6,
            0,
            0,
            0,  # length
            0,
            16,  # flags

            # labels[0] frame
            11,
            0,
            0,
            0,  # length
            0,
            0,  # flags
            108,
            97,
            98,
            101,
            108,  # labels[0]

            # labels end frame
            6,
            0,
            0,
            0,  # length
            0,
            40,  # flags
        ]
        # fmt: on

        self.assertEqual(bytearray(expected), msg.buf)