예제 #1
0
    def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        super(ResponseHeader, self).read(
            istream,
            kmip_version=kmip_version
        )
        tstream = BytearrayStream(istream.read(self.length))

        self.protocol_version = contents.ProtocolVersion()
        self.protocol_version.read(tstream, kmip_version=kmip_version)

        kmip_version = contents.protocol_version_to_kmip_version(
            self.protocol_version
        )

        self.time_stamp = contents.TimeStamp()
        self.time_stamp.read(tstream, kmip_version=kmip_version)

        if kmip_version >= enums.KMIPVersion.KMIP_2_0:
            if self.is_tag_next(enums.Tags.SERVER_HASHED_PASSWORD, tstream):
                server_hashed_password = primitives.ByteString(
                    tag=enums.Tags.SERVER_HASHED_PASSWORD
                )
                server_hashed_password.read(tstream, kmip_version=kmip_version)
                self._server_hashed_password = server_hashed_password

        self.batch_count = contents.BatchCount()
        self.batch_count.read(tstream, kmip_version=kmip_version)

        self.is_oversized(tstream)
        self.validate()
예제 #2
0
파일: engine.py 프로젝트: jseadragon/PyKMIP
 def _build_response(self, version, batch_items):
     header = messages.ResponseHeader(
         protocol_version=version,
         time_stamp=contents.TimeStamp(int(time.time())),
         batch_count=contents.BatchCount(len(batch_items)))
     message = messages.ResponseMessage(response_header=header,
                                        batch_items=batch_items)
     return message
예제 #3
0
    def test_handle_message_loop(self, request_mock):
        """
        Test that the correct logging and error handling occurs during the
        message handling loop.
        """
        data = utils.BytearrayStream()

        # Build a response and use it as a dummy processing result.
        batch_item = messages.ResponseBatchItem(
            result_status=contents.ResultStatus(enums.ResultStatus.SUCCESS),
            result_reason=contents.ResultReason(
                enums.ResultReason.OBJECT_ARCHIVED),
            result_message=contents.ResultMessage("Test message."))
        batch_items = [batch_item]
        header = messages.ResponseHeader(
            protocol_version=contents.ProtocolVersion(1, 0),
            time_stamp=contents.TimeStamp(int(time.time())),
            batch_count=contents.BatchCount(len(batch_items)))
        message = messages.ResponseMessage(response_header=header,
                                           batch_items=batch_items)

        kmip_engine = engine.KmipEngine()
        kmip_engine._logger = mock.MagicMock()
        kmip_session = session.KmipSession(kmip_engine, None, 'name')
        kmip_session._engine = mock.MagicMock()
        kmip_session._get_client_identity = mock.MagicMock()
        kmip_session._get_client_identity.return_value = 'test'
        kmip_session._engine.process_request = mock.MagicMock(
            return_value=(message, kmip_session._max_response_size))
        kmip_session._logger = mock.MagicMock()
        kmip_session._connection = mock.MagicMock()
        kmip_session._connection.shared_ciphers = mock.MagicMock(
            return_value=[('AES128-SHA256', 'TLSv1/SSLv3',
                           128), ('AES256-SHA256', 'TLSv1/SSLv3', 256)])
        kmip_session._connection.cipher = mock.MagicMock(
            return_value=('AES128-SHA256', 'TLSv1/SSLv3', 128))
        kmip_session._receive_request = mock.MagicMock(return_value=data)
        kmip_session._send_response = mock.MagicMock()

        kmip_session._handle_message_loop()

        kmip_session._receive_request.assert_called_once_with()
        kmip_session._logger.info.assert_not_called()
        kmip_session._logger.debug.assert_any_call(
            "Possible session ciphers: 2")
        kmip_session._logger.debug.assert_any_call(
            ('AES128-SHA256', 'TLSv1/SSLv3', 128))
        kmip_session._logger.debug.assert_any_call(
            ('AES256-SHA256', 'TLSv1/SSLv3', 256))
        kmip_session._logger.debug.assert_any_call(
            "Session cipher selected: {0}".format(
                ('AES128-SHA256', 'TLSv1/SSLv3', 128)))
        kmip_session._logger.warning.assert_not_called()
        kmip_session._logger.exception.assert_not_called()
        self.assertTrue(kmip_session._send_response.called)
예제 #4
0
    def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        super(RequestHeader, self).read(
            istream,
            kmip_version=kmip_version
        )
        tstream = BytearrayStream(istream.read(self.length))

        self.protocol_version = contents.ProtocolVersion()
        self.protocol_version.read(tstream, kmip_version=kmip_version)

        kmip_version = contents.protocol_version_to_kmip_version(
            self.protocol_version
        )

        # Read the maximum response size if it is present
        if self.is_tag_next(Tags.MAXIMUM_RESPONSE_SIZE, tstream):
            self.maximum_response_size = contents.MaximumResponseSize()
            self.maximum_response_size.read(tstream, kmip_version=kmip_version)

        # Read the asynchronous indicator if it is present
        if self.is_tag_next(Tags.ASYNCHRONOUS_INDICATOR, tstream):
            self.asynchronous_indicator = contents.AsynchronousIndicator()
            self.asynchronous_indicator.read(
                tstream,
                kmip_version=kmip_version
            )

        # Read the authentication if it is present
        if self.is_tag_next(Tags.AUTHENTICATION, tstream):
            self.authentication = contents.Authentication()
            self.authentication.read(tstream, kmip_version=kmip_version)

        # Read the batch error continuation option if it is present
        if self.is_tag_next(Tags.BATCH_ERROR_CONTINUATION_OPTION, tstream):
            self.batch_error_cont_option = BatchErrorContinuationOption()
            self.batch_error_cont_option.read(
                tstream,
                kmip_version=kmip_version
            )

        # Read the batch order option if it is present
        if self.is_tag_next(Tags.BATCH_ORDER_OPTION, tstream):
            self.batch_order_option = contents.BatchOrderOption()
            self.batch_order_option.read(tstream, kmip_version=kmip_version)

        # Read the time stamp if it is present
        if self.is_tag_next(Tags.TIME_STAMP, tstream):
            self.time_stamp = contents.TimeStamp()
            self.time_stamp.read(tstream, kmip_version=kmip_version)

        self.batch_count = contents.BatchCount()
        self.batch_count.read(tstream, kmip_version=kmip_version)

        self.is_oversized(tstream)
예제 #5
0
    def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        super(ResponseHeader, self).read(istream, kmip_version=kmip_version)
        tstream = BytearrayStream(istream.read(self.length))

        self.protocol_version = contents.ProtocolVersion()
        self.protocol_version.read(tstream, kmip_version=kmip_version)

        self.time_stamp = contents.TimeStamp()
        self.time_stamp.read(tstream, kmip_version=kmip_version)

        self.batch_count = contents.BatchCount()
        self.batch_count.read(tstream, kmip_version=kmip_version)

        self.is_oversized(tstream)
        self.validate()
예제 #6
0
    def test_handle_message_loop(self, request_mock):
        """
        Test that the correct logging and error handling occurs during the
        message handling loop.
        """
        data = utils.BytearrayStream()

        # Build a response and use it as a dummy processing result.
        batch_item = messages.ResponseBatchItem(
            result_status=contents.ResultStatus(enums.ResultStatus.SUCCESS),
            result_reason=contents.ResultReason(
                enums.ResultReason.OBJECT_ARCHIVED),
            result_message=contents.ResultMessage("Test message."))
        batch_items = [batch_item]
        header = messages.ResponseHeader(
            protocol_version=contents.ProtocolVersion.create(1, 0),
            time_stamp=contents.TimeStamp(int(time.time())),
            batch_count=contents.BatchCount(len(batch_items)))
        message = messages.ResponseMessage(response_header=header,
                                           batch_items=batch_items)

        kmip_engine = engine.KmipEngine()
        kmip_engine._logger = mock.MagicMock()
        kmip_session = session.KmipSession(kmip_engine, None, 'name')
        kmip_session._engine = mock.MagicMock()
        kmip_session._engine.process_request = mock.MagicMock(
            return_value=(message, kmip_session._max_response_size))
        kmip_session._logger = mock.MagicMock()
        kmip_session._connection = mock.MagicMock()
        kmip_session._receive_request = mock.MagicMock(return_value=data)
        kmip_session._send_response = mock.MagicMock()

        kmip_session._handle_message_loop()

        kmip_session._receive_request.assert_called_once_with()
        kmip_session._logger.info.assert_not_called()
        kmip_session._logger.warning.assert_not_called()
        kmip_session._logger.exception.assert_not_called()
        self.assertTrue(kmip_session._send_response.called)